Skip to content

Instantly share code, notes, and snippets.

@vaporwavie
Last active June 16, 2020 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaporwavie/8721acfd70054630209c2dc4ddd6c324 to your computer and use it in GitHub Desktop.
Save vaporwavie/8721acfd70054630209c2dc4ddd6c324 to your computer and use it in GitHub Desktop.
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