Todo List Codealong
Your First React Component
Install create-react-app
There's an excellent tool created by Facebook (Facebook made React) that will help you set up a barebones React app instantly. Let's use npm to install it globally so we'll always have it available in our Terminal.
Now that it's installed, let's move to the directory wherever you keep all your code and use the tool to start a React app. Replace my_app_name
with whatever you want the name your application directory.
Use npm start to start a server that will serve your new React application!
Notice that the tool creates a new directory named, like my_app_name
. Move into that directory and start Atom. Look around and see the structure the create tool provided.
One especially cool thing that we'll see is that this tool sets up our application so that the webpage automatically refreshes whenever we save any file in the directory. It's awesome.
Open the /src/App.js
file and change the text inside the <h2>
tag to say "Welcome to my first ever React App." Save the file and watch your browser automatically reload.
Our First Component
Add a new file in the /src
directory called MyComponent.jsx
. Notice that we're using a new file extension .jsx
which will allow us to write HTML and JS in a file at the same time.
Components are pieces of our application that we can define once and reuse all over the place. We have to use an import statement at the top of the file to make the file aware of React, and we have to use an export statement at the end of the file to make the code in this file available elsewhere in our application.
About our First Component
Here are some notes about our first component:
We used
React
to create the component by callingcreateClass
JSX syntax != HTML syntax. Note that in order to get the
well
class toappear, we needed to use the attribute
className
. React applies HTML classesto elements using
className
to avoid conflicting with JavaScript's ownclass
keyword. Here are some more details on JSX syntaxThe
render
function is what renders the component to the screen.
Put Your Component in the App
Go back to the App.js
. We're going to do two things to use our new component: 1. we have to import our component 2. we have to place our component on the page like an HTML tag like <MyComponent />
3. Save the file and see your page refresh and appear with HTML defined in your new component.
App.js ends up looking like this:
Nesting Components
Once components are created, we can use them as subcomponents. Let's try doing this by creating a list with list items inside.
Then, add <ToDoList></ToDoList>
to the render
function for MyApp
.
Our DOM structure should look like this:
Nice! These components are not only reusable, but they are semantic as well. But there's still no dynamic data associated with the elements, since we had to hardcode Item
in each ListItem
component.
Using Data through props
props
In order to represent and change data in React, we will first introduce the concept of props
. props
is how we can access data through properties on a component. If we compare the JSX syntax to HTML, these properties are passed by using attributes on the components. We can access props
like so:
Let's modify the ListItem
and ToDoList
components to use props
.
Note that we now pass {this.props.item}
into the render
function of ListItem
. This means that the property item
will be placed into the <li></li>
tags if we pass the property into ListItem
.
If we want to make this a truly extensible list, we could create an array of items, then pass them into props
through the ToDoList
component, then render out each item. Let's do that now.
Updating props
through state
props
through state
In a React component, state
is just another object, like props
. The only difference is that it can only be changed through the method setState
. The exception is setting the initial state, which is only done once when initializing a React component. In order to pass items to the ToDoList
component and make them mutable, we'll need to set the state of MyApp
. Let's continue refactoring our MyApp
component to assign toDos
through state
.
All we changed was adding a new function called getInitialState
, which returns the initial value of the state
object. We also altered the ToDoList
in MyApp
by passing this.state.toDos
into the items
property, which will then be accessed in the ToDoList
component through props
as usual.
Updating state
state
Updating state will involve calling setState
. Let's use a simple example with a clear button in MyApp
Now when we click on the button, the following will occur:
this.state
will be set to{toDos: []}
The
render
function forMyApp
will be called, and re-render the component
Let's add one more thing to our app, an input field for more items. In order to do so, we'll need the following variable to represent the new item we'll be entering:
newItem
We'll also need two additional functions to represent the following changes in state:
newItemChange
, for when we type characters into an input field and change the value ofnewItem
We'll need to get the current value of the input field and set
state
accordingly
addItem
, for when we submit the formWe'll need to make a copy of the
toDos
array, push thenewItem
, setstate
and clearnewItem
Like so:
Notes
Since JSX functions are callbacks, we can assume that any function called via an event can accept a callback argument with the event. We pass this in as
e
tonewItemChange
andaddItem
We can use
onChange
on theinput
field to trigger an event when the text in the box is changed
Keeping Modularity in Mind
So now we have a working list! But it could use some work. What if we want to use the MyApp
component again in our app? Since our actual list now requires four functions in the MyApp
component, we need to make sure that the list can stand on its own.
In order to fix this up, let's rename MyApp
to ToDoApp
and create a new MyApp
component so we can render multiple lists.
Final Solution
Last updated