1. 5
    Creating a RootState type and Typed Hooks for Type-Aware Redux Interactions
    1m 43s

Creating a RootState type and Typed Hooks for Type-Aware Redux Interactions

InstructorJamund Ferguson

Share this video with your friends

Send Tweet

This is probably the most boilerplate you'll be asked to write throughout this whole course. Just copy these few lines of code from the Usage with TypeScript section of the Redux docs into store.ts and hooks.ts and then use the useAppSelector and useAppDispatch hooks instead of the ones built-in to react-redux and you'll have solved 80% of your typing needs with Redux.

In addition to the typed hooks you'll use the RootState type any time you're creating a selector function and as we'll see later in this course sometimes with thunks as well It also may come in handy anytime you need to manually call store.getState().

The TS syntax I was most interested in learning for this lesson was ReturnType, which is a utility that extracts the return type from the type definition of a function. In our case ReturnType<typeof store.getState> creates a type that matches whatever data we have in our redux store, based on the various interfaces we define in each slice.

Ian Poston
~ 7 months ago

github button link responded 404 on this page. this is the correct link to this lesson https://github.com/xjamundx/redux-shopping-cart/tree/egghead/lessons/05-typed-redux