Last active
June 16, 2020 18:16
-
-
Save vaporwavie/8721acfd70054630209c2dc4ddd6c324 to your computer and use it in GitHub Desktop.
Demonstração do useSelector com tipagem de state customizada.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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