-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Memoize useDispatch? #1468
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
Comments
We decided not to ship any kind of a |
FWIW I also find that recommendation and example pretty misleading. Using I'm pretty sure that the following code, actually performs better than the code of the example, and it's a lot simpler: import React from 'react'
import { useDispatch } from 'react-redux'
export const CounterComponent = ({ value }) => {
const dispatch = useDispatch()
const incrementCounter = () => dispatch({ type: 'increment-counter' })
return (
<div>
<span>{value}</span>
<button onClick={incrementCounter}>Increment counter</button>
</div>
)
} |
I agree that it's not necessary for a |
The example with Before:
After:
What am I missing? Thank you very much |
Dispatch isn’t new every time. It’s the same (I think) as long as you have the same store. |
This statement is incorrect. The implementation is just: const useDispatch = () => {
const store = useStore();
return store.dispatch;
} You typically only have one Redux store instance for the lifetime of the app, so However, the ESLint rule doesn't know that - it just knows that |
Thank you for this clarification! This is great. I think I will submit a PR soon to improve the docs on this matter. However, before that, I wish to understand it a little better. You (@markerikson) said:
This alone does not imply that the function is always the same instance because it just shifts the problem into the stability of
The React docs do not explicitly state this; I guess it is probably obvious for someone minimally acquainted with it, but I have never used it so I don't know. That said, I will propose a PR. |
I... just explained under exactly what circumstances the store instance might change :) You didn't need to do any further digging around. And no, it's not a question of whether That's it. And in 99.98% of Redux apps, that store instance will never change throughout the life of the app. |
Yeah, I know, but I got curious 😬 Thank you.
Ah, ok! Makes sense! Thank you very much. Could you please review my PR? |
The docs say
with the example code being
I think this could use some clarification. I am understanding it to not be specific to useDispatch, and to just be the classic react thing of don't pass anonymous functions to children since they are different every render.
If that is the case, it would probably be worth highlighting that it's not specific to useDispatch. If I am misunderstanding, then is there any reason that react-redux can't export some helper that calls useCallback for you? I imagine most people would not realize they need to do this.
The text was updated successfully, but these errors were encountered: