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
The first step will be to break the app into components. Then, we'll think about how those components should connect to each other. This will give us an idea of where state should be.
-
0:00
The first step will be to break the app into components.
-
0:03
Then we'll think about how those components should connect to each other.
-
0:06
This will give us an idea of where its state should be.
-
0:08
Remember state is all the data we want to track in an app.
-
0:12
So when the user does any action, like type in a form, or check a box,
-
0:17
they are changing the state of the application.
-
0:21
And those changes will need to be stored as data somewhere.
-
0:24
As that data changes different components of the app will update what they
-
0:29
show to the user.
-
0:30
For example, when the user types a name into the main form of the app,
-
0:35
the app state changes to contain the string being typed.
-
0:40
And as that state changes,
-
0:42
the ghost component below updates itself with those changes.
-
0:47
So the components of the app need access to this state to perform these changes.
-
0:51
So we need to plan how they fit together to make this access efficient.
-
0:56
The best way to understand the component structure will be to look at what parts of
-
1:01
the app a user interacts with and how the app should respond as its state changes.
-
1:07
Anything the user will click or type in will likely be a component,
-
1:11
as well as anything that changes.
-
1:13
So let's start by looking at the app when it loads.
-
1:15
And I'll draw rectangles around the different pieces of
-
1:19
the user interface as we go.
-
1:21
So first, we know we'll need a component to contain the entire app.
-
1:25
So let's put a border around the whole thing.
-
1:29
And the first thing a user will interact with is this form.
-
1:31
So I'll draw a box around that.
-
1:35
And the counter is another component because it will need to update
-
1:38
itself as the state changes.
-
1:39
For example, when users submit new guests, delete guests,
-
1:44
check them confirmed and so on.
-
1:46
And the filter we check to hide unconfirmed guests will
-
1:50
also be a component.
-
1:52
And there are some hidden components we still need to find.
-
1:59
So let's add some names to the list.
-
2:03
And now we can see there are three new components, the name tiles.
-
2:07
And inside there are other parts that can change or accept user interaction.
-
2:12
Each one would contain four components.
-
2:14
We have the name, confirmed, check box and the two buttons, edit and remove.
-
2:19
Now it might seem that we've found all of the components now, but
-
2:23
there are actually a couple more.
-
2:25
Can you think of what they might be?
-
2:27
Well if I start typing a new name, there's this, what I like to call a ghost tile
-
2:31
that shows up indicating that there's a pending name for the list.
-
2:36
That will be a component too, because it shows or
-
2:38
hides itself depending on whether there is text in the form above.
-
2:42
It also updates itself with the text from the form.
-
2:46
So think about each of these name tiles here.
-
2:49
They are all really just the same component with different data or
-
2:55
names inside.
-
2:56
So we can just reuse one component for each name, but
-
2:59
to organize them in rows like this, we'll need another component to hold them.
-
3:05
So I'll draw a box around all these tiles to represent that parent component.
-
3:10
Now we've mapped out all our components,
-
3:12
and notice how some components are inside others.
-
3:15
Well this nesting shows how the components will be nested in our app.
-
3:19
For example, these purple boxes represent the children of the name tile components.
-
3:25
And these name tile components are the children of
-
3:28
the component we'll build to hold them, which is a child of the app itself.
-
3:32
So I've been color coding these to show you how they are all related to
-
3:36
each other.
-
3:36
For example, these orange boxes will be sibling components and
-
3:41
the green boxes are all these orange boxes' children.
-
3:45
So now let's think about how state will be shared.
-
3:49
When a name is typed into the form, the ghost tile here will need to have access
-
3:54
to that form, so it can know what to display.
-
3:57
So the name is an example of that state that will be shared by both the form and
-
4:03
ghost tile components.
-
4:05
Since these two components will need access to this state,
-
4:08
we're going to store that state in their closest shared ancestor.
-
4:12
In this case, that would be the main app.
-
4:14
The main app is the parent of the form and the grandparent of the ghost tile.
-
4:19
So that way there's a clear path to the state from both components.
-
4:23
So let's keep looking at other components to make sure this will work for them too.
-
4:28
The counter will need to change based on the number of guests in the state, right?
-
4:33
Well since we're planning on putting the state In the main app,
-
4:36
the counter will be able to access that from its parent.
-
4:39
But the counter also needs to know which boxes are checked in the name tiles below.
-
4:44
So as long as those components are updating the state in the main app,
-
4:48
the counter will be able to access that information.
-
4:51
And the more you think about it, any part of this app should just access
-
4:55
the state in the main component to either update it or read from it.
-
4:59
For example,
-
5:00
each of these remove buttons here should be able to remove the guest from a state.
-
5:05
And editing a name should alter the guest's name in the state.
-
5:09
So the main component makes the most sense, to hold the state,
-
5:13
since it's the nearest common ancestor to all these components.
-
5:17
Right, now that we've worked out a rough idea of our app's component structure and
-
5:21
where the state will live,
-
5:23
let's get specific about what we'll store in the state.
-
5:26
Then we'll set an initial state for the main app component.
You need to sign up for Treehouse in order to download course files.
Sign up