Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
When two or more components need access to the same state, we move the state into their common parent. This is called "lifting state up".
Resources
-
0:00
In React, two or more components can share the same state.
-
0:04
For example, the app component passes the players state
-
0:08
down to the header and player components through props.
-
0:13
Our application currently has two stateful components, app and counter.
-
0:19
But this is not a great overall structure for our application.
-
0:23
In the React basics course,
-
0:24
I just wanted to teach you how to create a simple stateful component, a counter.
-
0:29
The state of the counter is the player's score,
-
0:32
which is currently local to the component.
-
0:35
This state isn't available to other components in the app, which makes it
-
0:39
difficult to access and use player scores from other parts of our application.
-
0:43
In time, we'll need to access this state to determine the highest score and
-
0:47
the total score count that will be displayed in the header for example.
-
0:51
So when two or more component need access to the same state,
-
0:55
we move the state into their common parent.
-
0:58
This is calles lifting state up.
-
1:00
The score state is gonna be managed as high up in the application as possible.
-
1:04
So we'll lift the state of the counter component up to where the player data
-
1:09
lives in the app component.
-
1:11
This time we'll start from the bottom up.
-
1:14
Instead of keeping the score as local state,
-
1:16
we'll go back to letting it be a prop passed to the counter.
-
1:20
So in the counter class, let's delete the state object.
-
1:24
Let's also remove the button on click events for now.
-
1:28
In this video we're just focusing on moving state to our app component and
-
1:32
communicating the data downwards through props.
-
1:35
We'll handle the changing of data in the next video.
-
1:38
Let's also replace this.state.score with this.props.score.
-
1:45
So we've essentially converted the counter back to a stateless component
-
1:49
that just takes in props.
-
1:51
Now in the player component, we'll pass the score to the counter,
-
1:56
with a prop name score and
-
1:59
we'll set the value to props.score.
-
2:04
Then in the app components render method, let's pass the score to a player,
-
2:10
also with the prop name score, to keep the prop names consistent, and
-
2:14
as the value, we'll write player.score.
-
2:18
Finally, let's initialize a score state here at the application level.
-
2:23
Add a score property to each player object and set it to zero by default.
-
2:34
If we run our code, we should see that it displays our initial player state,
-
2:39
much like it always has.
-
2:40
However, our dataflow is different.
-
2:43
Our only stateful component is now app.
-
2:46
Our counter component is now simpler and stateless.
-
2:49
Now you could leave it as a class, but
-
2:51
if you'd like you can convert the counter back to a functional component since
-
2:55
it's only going to receive input through props and render UI.
-
2:58
I'll show you how to quickly convert it.
-
3:00
First, I'll change the import statement to just import react, from react, then
-
3:06
replace the class definition with an error function that takes a props parameter.
-
3:16
I'll remove the render method and keep the return statement only.
-
3:23
And I'll comment out both event handlers for now.
-
3:26
We'll refactor these into one function in the next video.
-
3:30
Finally, I'll change this.props.score to just props.score.
-
3:36
Over in the scoreboard, pressing the counter buttons no longer modifies
-
3:40
the score, but this is to be expected right now, and we'll fix that next.
You need to sign up for Treehouse in order to download course files.
Sign up