1. 7
    Create a Reducer with Redux Toolkit and Dispatch its Action with the useAppDispatch hook
    2m 3s

Create a Reducer with Redux Toolkit and Dispatch its Action with the useAppDispatch hook

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

In react-redux the useDispatch hook gives us access to our store's dispatch method. Dispatch is used to send actions into our redux store and is the only way we can affect the store from within a component.

const dispatch = useAppDispatch();
dispatch({ type: "featureName/actionName, payload: {} })

The only works with function components. Class components cannot use hooks and need to use the legacy connect higher order component and a mapDispatchToProps function to gain a way to dispatch actions from components.

Actions with redux are recommended to follow the flux standard action format, which always includes type field and usually a payload key containing the primary data associated with the action.

Jamund Ferguson: [0:01] In productsSlice.ts, make sure to clear out any fake products you created in your initialState, and down here in reducers let's add a new reducer method called receivedProducts. That method is going to take state, and it's also going to take something called an action. It's going to have the type of PayloadAction.

[0:16] PayloadAction is a special type that we're going to import from react.js/toolkit. That type takes in as an argument the value that we want our action payload to look like. In this case, our payload is going to be an array of Product.

[0:30] Let's save those products into a variable called products, and let's convert them into an object by looping over them. For each product we'll say state.products [product.id] = product. We're essentially converting our product array into a products object.

[0:52] Now that we've created that reducer method, we can export an action for it by typing export const { receivedProducts } = productsSlice.actions. Redux Toolkit automatically generates action creators for each of the reducer methods that we pass into it.

[1:10] Let's go back to products. Let's import { receivedProducts } from "./productsSlice". We also need to import { useAppDispatch } from "../../app/hooks".

[1:19] Here at the top of the Products() component, type const dispatch = useAppDispatch(). We'll then remove our useState line here. We'll bring our useEffect hook back into it, but instead of calling setProducts, once the products are received, we are going to call dispatch(receivedProducts(products)).

[1:38] You can see how dispatching this into Redux isn't that much harder than setting products on the local component state. It's two methods instead of one, but at the end of the day, it's just a single line of code that's pretty easy to read. Now we can remove the unneeded useState import and the unneeded Product import.

[1:54] If we go back to our products page, it's fully working, but instead of being powered by local component state, it's now powered by Redux.

egghead
egghead
~ 14 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