From the course: Learning Redux Toolkit

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Getting data from your store with the useSelector hook

Getting data from your store with the useSelector hook

From the course: Learning Redux Toolkit

Getting data from your store with the useSelector hook

- [Instructor] To render our cart buttons conditionally, we'll need to get a cart data from the Redux store. We only want to show the add to cart button if an item is not in our cart. And if it's already in our cart, we'll display the remove from cart button instead. First, we need to get all the product IDs in our cart state. And we'll do this by using the useSelector hook from react-redux. On line one, along with the useDispatch hook, let's import the useSelector hook. Next in the Home component, we'll create a new line and a variable named state. As its value, let's call the useSelector hook and pass a function as its argument. We'll provide a parameter to our function, and make the parameter our function's return value. This return value will be our Redux store data, so we'll name it state. Now we can access our cartProductIds from state.cart.productsIds. We're able to access our Redux store with the…

Contents