Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstração do useSelector com tipagem de state customizada.
// store.tsx
import { AppState } from 'types' // local lib
import {
useSelector as useReduxSelector, // mask useSelector first import
TypedUseSelectorHook, // declare use selector generic types
} from 'react-redux'
import { configureStore } from '@reduxjs/toolkit'
import reducer from './reducers'
// override default state type with our customized one
export const useSelector: TypedUseSelectorHook<AppState> = useReduxSelector
export default configureStore({ reducer })
// demo.tsx
import { useSelector } from 'store'
import { useDispatch } from 'react-redux'
const { foo } = useSelector((state) => state.Application) // wowza! now your selector is able to identify your typed state by default!
return <span>{foo}</span> //? <span>this is so cool</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment