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

Infinite recursion occurring with middleware #1564

Closed
jbri7357 opened this issue Apr 1, 2016 · 3 comments
Closed

Infinite recursion occurring with middleware #1564

jbri7357 opened this issue Apr 1, 2016 · 3 comments
Labels

Comments

@jbri7357
Copy link

jbri7357 commented Apr 1, 2016

I'm working on a webapp that's utilizing React with Redux. I have posted a similar message at the redux-thunk Github, but I think this might possibly be a more general issue with Redux middleware. I am currently using Redux-Thunk in a manner very similar to the example found here:
http://redux.js.org/docs/advanced/ExampleRedditAPI.html

When running my app, I see the following error in Chrome:

Uncaught RangeError: Maximum call stack size exceeded
(anonymous function) @ index.js:10
dispatch @ applyMiddleware.js:44
(anonymous function) @ index.js:12
dispatch @ applyMiddleware.js:44
(anonymous function) @ index.js:12
dispatch @ applyMiddleware.js:44
(anonymous function) @ index.js:12
dispatch @ applyMiddleware.js:44

Here's a link to my project:
https://github.com/jbri7357/redux-sample-app

@gaearon
Copy link
Contributor

gaearon commented Apr 1, 2016

I would say that given their level of usage, it is very unlikely to be an issue with either Redux or Redux Thunk, and much more likely to be a subtle mistake in the app’s code.

It’s hard to diagnose from the source alone. If you could publish a project reproducing the issue, that would be helpful.

@gaearon
Copy link
Contributor

gaearon commented Apr 1, 2016

The issue is here:

const mapDispatchToProps = (dispatch) => {
  return dispatch;
}

Just like mapStateToProps, mapDispatchToProps is supposed to return an object with props.

If you supply a function, connect() will call that function to obtain an instance-specific mapDispatchToProps() (this is useful for per-instance memoization). This is not relevant to your problem though.

In your case, you meant to return {dispatch} (an object) instead of dispatch (a function):

const mapDispatchToProps = (dispatch) => {
  // return dispatch;
  return {dispatch};
}

That will fix the problem. By the way, this is the default behavior if you don’t specify mapDispatchToProps, so you might as well delete it completely and not pass it to connect() at all.

I hope this helps!

@gaearon gaearon closed this as completed Apr 1, 2016
@jbri7357
Copy link
Author

jbri7357 commented Apr 1, 2016

Thank you so much for your help with this and the thorough explanation you provided!

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

No branches or pull requests

2 participants