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

Calling dispatch method immediately results in "Reducer may not dispatch actions" #122

Closed
DaBs opened this issue Jan 5, 2017 · 8 comments

Comments

@DaBs
Copy link

DaBs commented Jan 5, 2017

As title states, calling a dispatch action with the current version (2.1.0), will result in redux throwing the error "Reducers may not dispatch actions" from here. It seems that since the call is immediate, that redux considers this to be a part of the reducer function it is currently executing. Encapsulating the dispatch call inside a setTimeout for just a single miliseconds works fine and throws no error.

Example:

export const fetchItems = () => {
  return dispatch => {
    dispatch({type: 'FETCHING_ITEMS'}); // Will throw error

    setTimeout(() => { dispatch({type: 'FETCHING_ITEMS'}); }, 1); // Works flawlessly

    // Rest of function code here that works flawlessly
  }
}

My use-case is updating the current state with isFetchingItems: true by dispatching the first function and then dispatch another function afterwards when the fetch has returned.

@artemjackson
Copy link

It's probably redux issue, please move it to redux repository and specify redux version.

@lbrdar
Copy link

lbrdar commented Jun 8, 2017

Any news on this?

I have an action to handle login and then change route to new screen where component needs to fetch some data in componentWillMount() by dispatching an action to do so. This fails unless I use setTimeout, as DaBs showed above.

redux 3.6.0
redux-thunk 2.2.0

@DaBs
Copy link
Author

DaBs commented Jun 8, 2017

@lbrdar As artemjackson said, I believe it's probably a redux issue, but in conjunction with thunk (or any other middleware for that sake), however it should be fixed in redux. This can be closed, but should be considered to be reopened at the redux repo.

@DaBs DaBs closed this as completed Jun 8, 2017
@LaloHao
Copy link

LaloHao commented Nov 1, 2017

Any update on this?

@benjiwheeler
Copy link

FWIW, I was seeing these errors when I put a debugger statement inside my reducer. Seems like there are assumptions redux is making when deciding that this error condition has been met that are either incorrect, or which involve complexity in the js execution innards.

@timdorr
Copy link
Member

timdorr commented Oct 2, 2018

@benjiwheeler You shouldn't put debugger statements in a reducer because that makes it non-pure. Instead, put them prior to your dispatch calls, or in whatever you have subscribed to the store. When you stop execution like that, Redux still thinks it's within the dispatch because it is normally synchronous code. debugger turns it into async code, essentially.

@navdeeshahuja

This comment has been minimized.

@nzey
Copy link

nzey commented Apr 24, 2020

Call the dispatch function like:

setTimeout(() => { dispatch({type: 'FETCHING_ITEMS'}); }, 0);

Why was this comment marked "resolved"? I would expect using 0, instead of 1, would also work. Does it?

@reduxjs reduxjs locked as resolved and limited conversation to collaborators Apr 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants