This minor release adds a new syntax for pre-typing hooks.
.withTypes
Previously, the approach for "pre-typing" hooks with your app settings was a little varied. The result would look something like the below:
import type { TypedUseSelectorHook } from "react-redux"
import { useDispatch, useSelector, useStore } from "react-redux"
import type { AppDispatch, AppStore, RootState } from "./store"
export const useAppDispatch: () => AppDispatch = useDispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
export const useAppStore = useStore as () => AppStore
React Redux v9.1.0 adds a new .withTypes
method to each of these hooks, analogous to the .withTypes
method found on Redux Toolkit's createAsyncThunk
.
The setup now becomes:
import { useDispatch, useSelector, useStore } from "react-redux"
import type { AppDispatch, AppStore, RootState } from "./store"
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
export const useAppSelector = useSelector.withTypes<RootState>()
export const useAppStore = useStore.withTypes<AppStore>()
What's Changed
- Update hooks.md — reselect usage with multiple instances simplified by @VorontsovIE in #2110
- Modernize ESLint configuration by @aryaemami59 in #2115
- Introduce pre-typed hooks via
hook.withTypes<RootState>()
method by @aryaemami59 in #2114
New Contributors
- @VorontsovIE made their first contribution in #2110
Full Changelog: v9.0.4...v9.1.0