diff --git a/src/store/hooks.ts b/src/store/hooks.ts new file mode 100644 index 00000000..a1a7827b --- /dev/null +++ b/src/store/hooks.ts @@ -0,0 +1,7 @@ +import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; + +import type { RootState, AppDispatch } from './index'; + +// Use throughout your app instead of plain `useDispatch` and `useSelector` +export const useAppDispatch: () => AppDispatch = useDispatch; +export const useAppSelector: TypedUseSelectorHook = useSelector; diff --git a/src/store/index.ts b/src/store/index.ts index bb43882b..2232ac51 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -31,3 +31,8 @@ export const middleware = (getDefaultMiddleware: Function) => ); export const store = configureStore({ reducer, middleware }); + +// Infer the `RootState` and `AppDispatch` types from the store itself +export type RootState = ReturnType; +// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} +export type AppDispatch = typeof store.dispatch;