1. 6
    Access Redux Data in a Component with the TypeScript-enabled useAppSelector hook
    1m 21s

Access Redux Data in a Component with the TypeScript-enabled useAppSelector hook

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

The useSelector hook from react-redux is incredibly easy to use, but doesn't work super well with TypeScript. The useAppSelector function we created in the last lesson solves this by telling it that state is always going to be of the RootState type we created.

const products = useAppSelector((state) => state.products.products);

This only works with function components. Class components cannot use hooks and need to use the legacy connect higher order component to gain access to data in the redux store.

Once we type the useAppSelector method and our individual slices we don't need any special type syntax as its all covered by the RootState type we defined earlier.

Anytime the redux store changes, the selector function passed in to useAppSelector will be executed. If the value returned is not an exact match to the last returned value, redux will force the component to re-render.

useSelector/useAppSelector can also take a second argument, an equality function. Using _.isEqual from Lodash for example could help prevent additional renders in some cases. react-redux also includes a shallowEqual method that will compare all of the key/value pairs of an object one layer deep. That works well if your selector return an object made up of strings, numbers or booleans.

import { shallowEqual, useSelector } from 'react-redux'
const selectedData = useSelector(selectorReturningObject, shallowEqual)

Jamund Ferguson: [0:00] The Products page in our application is made up of data dynamically loaded from a JSON file. That call happens inside of Products.tsx inside of this useEffect hook, which calls getProducts and then sets them into local component state. We're going to comment out this code and pull the products data from Redux instead.

[0:17] At the top of the file import { useAppSelector } from "../../app/hooks". Then, down in our component, type const products = useAppSelector. Let's pass in an arrow function which takes in the state and let's let TypeScript complete the rest. We type state, we select products, and we want products. We can see here also that because we use useAppSelector, our products list is typed properly.

[0:45] Because we store products in Redux as an object and not an array, we have to update how we map over this. We can simply type Object.values to convert that object into an array of products. With that in place if we go back to the Products page, you'll see an empty screen.

[1:02] We can test that it's working by going into productsSlice and in our initial state adding one fake product. We'll give it an id of 123 and a name of "fake product". We'll save that. You'll notice TypeScript is mad because it's missing a few values, but it's good enough to power a fake product on our page.

egghead
egghead
~ 20 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today