How to properly type useSelector and useDispatch from react-redux?
See original GitHub issueHi everyone, I’m new to typescript and I’m using easy-peasy with react-redux. I don’t know what is the best way to type useSelector and useDispatch. As far as I know, I can manually define the RootState interface and pass it to useSelector. Something like the code below
interface RootState {
count: number;
}
const {number} = useSelector((state: RootState) => state)
It works but then I have to change the interface every time I change the models. Is there a way to auto generate RootState interface from the model interface?
Another thing is the useDispatch, how can I make auto-complete work for the following code?
// auto-complete for someAction, someThunk
const {someAction, someThunk} = useDispatch()
I would appreciate it if someone could help me. Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
UseSelector and UseDispatch: A Guide to React-Redux Hooks
This guide will cover how to implement the React-Redux hooks useSelector and useDispatch in your application.
Read more >React-Redux Hooks: useSelector and useDispatch
A guide to using Redux with React hooks for cleaner code.
Read more >Usage with TypeScript - React Redux
Usage > TypeScript: how to correctly type React Redux APIs. ... of the useDispatch and useSelector hooks for usage in your application.
Read more >How to use useDispatch and useSelector? React Hooks ...
For this, we have to install redux and implement store, action, and reducer. Following is the action: const increment = { type: "INCREMENT"...
Read more >React-Redux useSelector typescript type for state
This might not be the answer but I use it like so: const isLoggedIn = useSelector<IRootState, boolean>(state => state.user.loggedIn);.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You can most certainly achieve this. I am away from my keyboard for most of the day, but will try answer as soon as possible.
Hey @dpyzo0o
Better late than never. 😅
Here is a CodeSandbox that shows how to create and use typed versions of the
react-reduxhooks.I’ll make sure we add this to the recipes. 👍