Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select() function does not work with use-saga-reducer #29

Closed
evanrolfe opened this issue Nov 20, 2019 · 4 comments
Closed

Select() function does not work with use-saga-reducer #29

evanrolfe opened this issue Nov 20, 2019 · 4 comments

Comments

@evanrolfe
Copy link

The problem:
Calling yield select() inside a saga always returns the initial state, and not the actual current state. I need to access the current state inside a saga function, is there a different way to do this or is this a bug that needs fixing?

Steps to reproduce:
In examples/13_saga/src/store.ts, import select from redux-saga/effects:

import {
  call,
  put,
  delay,
  takeLatest,
  takeEvery,
  all,
  select
} from 'redux-saga/effects';

And change delayedDecrementer() to be:

function* delayedDecrementer() {
  const count = yield select(state => state.count);
  console.log(`The count is: ${count}`)
  yield delay(500);
  yield put({ type: 'DECREMENT' });
}

Now, open the app in the browser, click the plus button a few times so the count equals 3 (or any number > 0). Then click "-1 Delayed" button.

What actually happens:
The console logs display the message The count is: 0.

What should happen:
The console logs should show the actual count value before the decrement i.e. The count is: 3.

@dai-shi
Copy link
Owner

dai-shi commented Nov 20, 2019

Hi, thanks for reporting.
It looks like a bug in use-saga-reducer.

if I add

    useEffect(() => {                                                      
      stateRef.current = state;
    });

right after const stateRef = useRef(state);,
it seems to work.
(I'm not sure if useEffect runs too late and this case requires useLayoutEffect.)

@azmenak Would you take a look please?

@evanrolfe
Copy link
Author

@dai-shi thank you for the quick response!

I can confirm that adding that useEffect() call does seem to fix the problem.

@dai-shi
Copy link
Owner

dai-shi commented Dec 1, 2019

Closing this as I filed the issue in the upstream: azmenak/use-saga-reducer#1

@dai-shi dai-shi closed this as completed Dec 1, 2019
@azmenak
Copy link

azmenak commented Dec 20, 2019

Update: issue has been resolved in use-saga-reducer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants