Lesson 05
mapStateToProps and Creating the FruitList component - Learn Redux Saga Tutorial
Summary
Redux Setup and Fruit List Component Creation
Overview
In this lesson, the instructor walks through setting up Redux to manage a list of fruits, demonstrating how to connect state to props in a React application and create a simple functional component to display the fruit list.
Key Points
-
Redux Overview:
- The course focuses on the middle area of Redux, assuming prior knowledge of Redux concepts.
- A quick review of Redux principles is provided.
-
Defining
mapStateToProps
:mapStateToProps
is defined to access thefruits
property from the Redux state.- The fruits can be accessed using
state.fruits
, which gets injected as a prop calledfruits
.
-
Console Logging Props:
- A console log is added to the
random
method inApp.js
to verify thatfruits
is included in the props. - Observations about props in the console show that both
dispatch
andfruits
are present.
- A console log is added to the
-
Connecting Redux State:
- The importance of properly connecting state to props is emphasized, and the instructor shows what happens if nothing is passed to the connection.
-
Creating Fruit List Component:
- A new component called
FruitList
is created to render the list of fruits. - A stateless functional component is defined to take in props and return a template for rendering a list.
- A new component called
-
Rendering the Fruit List:
- The
FruitList
component maps overprops.fruits
to create an unordered list (ul
) of fruit names, assigning eachli
a key based on the fruit's ID.
- The
-
Importing and Using the Component:
- The
FruitList
component is imported intoApp.js
and used to display the fruits. - The final output in the browser shows the fruits displayed correctly in a bullet list.
- The
Conclusion
- The instructor confirms the successful setup of Redux and creation of the
FruitList
component. - A reminder to take a break before moving on to the next lesson is given.
Video Transcript
Welcome back everyone. So I'm trying to go a little bit quicker here and setting up
Redux because I assume we already have some knowledge of Redux and this
course is not really about Redux but about the middle area Redux. Even though
I'm trying to go fast I'm trying to also let you know of everything and give you
a review of Redux if you don't remember things clearly. Anyway I don't want to
skip or step over things too fast. Let's keep going then. So let's define map
state to props right here. So right now if you look back at the Redux now we
have an initial property fruits with an array of objects. That's great. So we go
back to app.js. We define map state to props. Now I want to be able to access
the list of fruits using the property fruits. Could be any property name you
want. So how can I get the fruits? Well that's in the state right but the state
has is an object that has what the fruits property. So we can just say that
state.fruits. And that will give you the fruits. That's it. So this guy will be
injected as prop called fruits so we can access it with this prop.fruits.
Great that's good. Let's do a console log in the random method here for the app.
How about that? So under the random method put a console log and say this
prop props like so. We just want to know what's in the props for this given to app.
And we're gonna see we hope that fruits is there. Now let's save it. Notice the
console log is under render. Don't make the mistake of doing this. Alright. So
let's get back to the browser. So I already have my console right here at the
bottom. If you take a look there's this prop right the console log is an object
and you can see oh this object has two properties. The first property is called
dispatch which is a function. And this is a function that's used to emit actions
right, to reduce actions. And you can see also the fruits is here. So if you go
back to here that's from app state to prop. So you couldn't have pass null. What
happens if pass null pass nothing or nothing right. See what happens. So I
have nothing to connect the first set of parentheses. I'm gonna see what's
happening here. You'll see the average only has dispatch now. So connect added
the prop dispatch so we can dispatch actions. Now I'll go back to add on here.
I'm gonna revert and add the map state to props there. So we can see finally
finally the fruits we can access it using this prop's dot fruits right here. See
Apple origin strawberry. That's great. So we hooked up redux successfully.
Great. Anyway let's keep going. So that is great. So we hooked it up successfully.
Now we want to be able to pass it to the list of fruits. Now we kind of wanted to
do the step by step. But let's do step four right here right now. How about that?
But using fake data. So in place of this fruit list we're gonna make an actual
fruit list component. So we're gonna call it fruit list like so. And then we're
gonna pass a prop right. I'm gonna say fruits. And I'm just gonna pass this
props to this props that fruits. That's it. But we have to define this component.
So let's do it as our secret new file got fruit list.
fruit list.js in capital case. Let's make this guy a stateless functional
stateless functional component because we don't really care about keeping state
and it's pretty simple. Okay. How about that? So let me make a function.
call it fruit list. So function that takes first argument props. And then you
return the template for this guy. So this is a stateless functional component.
It's very simple. It just takes one parameter called props and the return is
actually the render equivalent to the rendered function. Right. We're just gonna
return the ul on our list. And we're gonna map props.fruits. We're gonna generate for
each fruit li component. Okay. So a fruit. We're gonna return li component.
With the fruit.name. Since we are doing map react will warn you you need a key. So let's
use the fruit ID as the key. Now it's all good but we have to always import react from react
because we're using jsx right here. And okay. Now finally if we export you can just
default if you want the fruit list component. Okay. So let's review what we did here. So we
created a component just just a function that props. That's the props it receives as the first
argument. So we return a template with a ul. Now we want to generate li component element or
component for each fruit. So fruit is given as props props out fruits. So for that we use
the map function that would take every fruit and generate a list right with li's. Now we use a key
the fruit ID as key because otherwise react will complain about not having key. And we use the fruit
name as the actual content of this element. Notice I'm using many shorthands of ESX so here
you don't really need the parenthesis. It's optional if you only have one argument. Here too
same thing okay. And here is an implicit return. I don't have the keyword return but it's implicitly
because it's a narrow function and I implicit return an li. Okay. It's actually
same as this guy. All right. Anyway probably know that or not. Let's save this though that's great.
Looking good this fruit list component very simple. Let's go back to app. Now we using fruit list here
past the fruits as prop right. But we need to import it because it's saying it's not defined. So let's
do it. We're going to go up here before I touch it. Yes. So whatever import fruit list from
dot slash fruit list because it's in the same directory.
That's great. Let's test it out. Go back to the browser. So now you can see apple orange
is strawberry in a bullet list. That's great. Everything worked fine. So we have finished this
lesson. We hooked up the maps day two props and we successfully created the fruit list component.
Okay. So let's take a break and I'll see you in the next lesson.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: