question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to properly type useSelector and useDispatch from react-redux?

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
ctrlplusbcommented, Nov 29, 2019

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.

1reaction
ctrlplusbcommented, Apr 12, 2020

Hey @dpyzo0o

Better late than never. 😅

Here is a CodeSandbox that shows how to create and use typed versions of the react-redux hooks.

I’ll make sure we add this to the recipes. 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found