-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add useDispatch() variant for dispatching async thunks #1694
Comments
I'm not quite sure what you're proposing here.
If you're getting some kind of a "can't update component A while rendering component B" error, you have some other bug in your code. There is a complication with https://redux-toolkit.js.org/tutorials/typescript#define-root-state-and-dispatch-types |
I agree it's probably a bug in my code. However, I still feel the need to use this custom hook because of this discussion, and the StackOverflow reference I mentioned in my original comment. As per my understanding, dispatching an async thunk (or perhaps any action) before a functional React component has first rendered causes this bug to occur. So, the developer must ensure the dispatch gets delayed using |
Yeah, I'm really not sure what the problem statement is here that you're describing. It would help if you could show an example of the actual code you're concerned with You definitely should not be dispatching actions directly while rendering, if that's what you're asking: function MyComponent() {
const dispatch = useDispatch();
dispatch(anything()) // DON'T DO THIS IN THE RENDER LOGIC ITSELF!!!
return <div />
} This applies for any Redux action (plain action or thunk). Or, for that matter, React state setting. Rendering logic should be pure - just |
Actually, that code is also incorrect for the library. It will immediately dispatch when that hook is used, regardless of your intent of what to do with dispatch. That's not what our API should do. We provide the functions, you figure out when to call them. |
New Features
I propose either adding a variant of
useDispatch()
(which I'll refer to asuseThunkDispatch()
throughout this issue), or updatinguseDispatch()
to handle both sync and async actions.What is the new or updated feature that you are suggesting?
Currently, I've created the following React Hook to use in my codebase. Note that I'm using Redux Toolkit, React Redux, and TypeScript in my codebase.
Why should this feature be included?
Currently, we use
useDispatch()
to dispatch actions but it doesn't work out of the box for async thunks. If you useuseDispatch()
directly, you get the following error (I've replaced the actual component names with<ComponentName>
):This error can be fixed by using
useEffect()
to ensure the action is only dispatched once (StackOverflow reference). With either the addition ofuseThunkDispatch()
or the updation ofuseDispatch()
to handle this case, we needn't worry about this anymore.What docs changes are needed to explain this?
https://react-redux.js.org/api/hooks must document the new/updated hook.
The text was updated successfully, but these errors were encountered: