Complete React Tutorial (& Redux ) #42 - Map Dispatch To Props

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

Shows how to map dispatch to props in a React & Redux tutorial with a delete action example

Full Transcript

alright then so so far we've mapped the state to our components like this map state two props and we've read data from the states but what if we want to change the state what if we want for example to delete one of these posts on this page a little delete button or something to delete that then what we need to do is interact with the state from this component now if we refer back to this diagram we have our central state and we've already passed our data via the props to this component and that's using that function map state two props now if we want to make a change to the state we have to dispatch an action from the component and that action is going to contain a type for example delete post or add post or whatever and it's going to contain an optional payload and in the case of deleting a post it could be that that payload is the ID of the post we want to delete so then that action is dispatched to the reducer the reducer takes the action checks the type of action takes in the payload the ID that we want to delete from the states and it makes that change to the central state then when that changes we get the updated props in side the component alright so we need to figure out a way to dispatch an action from the component right here and that's pretty simple to do so like before when we did map state to props this time what we're going to do is map dispatch to props and that's going to map our dispatches to our props so that we can call them from our component so let's create a Const Westphal and call this map dispatch - props now this is an aryl function and we'll come back to those parentheses in a minute because it takes in a parameter and this parameter is the dispatch method now remember when we worked in code Ben and we were looking at dispatches we dispatched an action like this we said the store and then dispatch so dot dispatch like so and we passed in an action with a type and some additional payload so that's how we did it then but what we're doing now is we're mapping dispatch to props so were passing this function which is on the store through into this function right here as a parameter which means inside here we could just say if we wanted to dispatch and then the action we don't need to say store dispatch anymore okay so what we want to do in here is return an object and this object is going to represent like it did here what properties are what functions where get a map to the props of this component so I want to map a function called delete post because we're going to delete a post inside this component which is the component detail by the way so this delete post right here what I wanted to do is dispatch an action so it's going to be a function first of all which will take a parameter and that parameter is going to be the ID of the post that we want to delete so it's an error function so inside here let us do our function contents and what we'd like to do is dispatch an action like this now the type of that action this is going to be delete underscore post and the additional payload we want to send is the ID so I'll create a property called ID and set that equal to the ID right here that we receive inside this function when we call it okay then so what we're doing now is we're dispatching this action whenever we call this function and this function is going to be attached to our props so we can use it inside the component but before we do that we need to take this and we need to pass it into the connect function down here much like we did with map state to props so map state two props first map dispatch two props second alright then cool so now if we go up to the component over here what I'm going to do is just log to the console the props so we can see that function on the props so console.log and then it's this dot props all right so save that check this out in a browser now we see the props object right here and we can see that we have this delete post method right here and it expects an ID so this is the method we'll be calling to delete a post from this post detail component alright so let's do that first of all we need some kind of button underneath here so that we click that and then it's gonna fire this function at some point so let's create that underneath this PITA so we'll do a div first of all with a class of center just to centrally align the button inside this button we'll have a class of button this is a materialized CSS class also a class of gray to color it gray all right so inside we'll just say delete post now we want to attach a click event to this button so let's say on click and set that equal to some function or other which will define up here and that function is going to be called handle click so I'll say this to reference the component dots handle clicked like so all right so let's create that function handle click you call a function and inside that function we'd like to call this method right here because we want to delete the post so we can just say this dot props delete post like so and call that function but remember we pass in the ID it expects an ID because we send it inside the action so we need to send the ID of this post now if you look on the props on the post right here we have an ID property so we could just grab that by saying this props dot post ID then we're passing the ID of that post into this function okay so what we're doing is we're calling this function which is in turn firing this function right here and inside that function we're making a dispatch and sending this action to the route reducer so let's save that and go to the route reducer because this is where we receive the action so for now let's just log this to the console will say console dot log and then the action so whenever that action is dispatched whenever we click on that delete button then it's gonna receive that action right here and it's going to log it to the console so let's save it and see if this works so delete post all right so it's logging that to the console do it again and it logs it again every time we click delete post it's firing that function dispatching the action to the root reducer and logging it to the console so that's good so far but what we actually want to do is delete this from the state so to do that first of all we have to check the type of action because we don't want to delete it for every action just if the action type is delete post so let's perform a little check right here will say if action type is equal to delete wonder score post then we're going to do something and this thing that we're going to do is update the state now remember when we update the state we don't want to do anything destructive we don't want to alter the original state we want to work out a non destructive way to do this and we can do that using the filter method because the filter method doesn't alter the original array it creates a new array so we could say let new posts equal to state dot posts dots filter and we're going to filter through these posts so the filter method performs a function on each individual post and if we return true for that function then we keep that post in the new array over here if we return false then we filter that post out of the new array so this callback function takes the individual post that we're cycling through at the time and what we want to do is check does this post ID equal to the ID we receive on the action if it does equal the same thing then what we want to do is return false because that will filter it out of the array we want to filter it out if they are the same we want to return true if the action ID and the post ID and not equal so let's say we turn action dots ID is not equal to post to ID so this is going to be true with these two things and not the same the IDS on these two things all right I remember we get the ID on the action because we passed it in right here okay so then what we're doing now is creating this new array which should have filtered out the post that we want to now we need to return a new object which represents the new state so we return that right here now remember we can't just say return and then post is equal to new posts we could do in this case but if in the future we had more properties like users which was equal to something and then you know something else then what this is going to do is overwrite all of that state object with this object right here which we don't have a users property on so we don't want to do that first of all we want to take the current state and we want to spread it like that so that all of the properties from the state are returned inside this object first then we override the post property with the new posts okay that's what we need to do so let's save that now and see if this works so if we come over here and delete post then you see the post goes right and if we go back to the home page that post is no longer there now this is okay but what I'd like to do is that when you delete a post it redirects you to the home page instead of just seeing this thing right here so let's do that we do that inside the component so if we come up to where we delete the post this is where I'll redirect the user after this thing has been done so down here we can say this dot props history and then use the method called push record this in a previous tutorial this is how we can redirect someone to another page and we want to redirect them to just forward slash which is the home page so if we save this now all the data's should be back go back to the home page and if we click on one of these delete it go back to the home page and it's not there anymore cool delete that sweet and finally that awesome okay then my friends so there we go that is how we map dispatch like this to props how we call that dispatch from the component itself when that dispatch is received in the reducer we can update the state

Original Description

Hey gang, in this React & Redux tutorial I'll show you how we can map a dispatch to our component props, so that we can fire a dispatch from our components. In this tutorial we'll be dispatching a delete action, to delete a blog post from our Redux store. ---------------------------------------- 🐱‍💻 Course Links: + VS Code editor - https://code.visualstudio.com/ + GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist + React CDN - https://reactjs.org/docs/cdn-links.html ---------------------------------------- 🤑 Donate @ https://www.paypal.me/thenetninja 🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →