Exercise: React State and Props
Objective#
Your objective is to create a todo list that can be viewed by the user, and a form where the user can add new items to the todo list stored in state.
Getting Started#
This is part 2 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 1.
Steps#
React Series Part 2 has multiple exercises
- Exercise 1: Project Structure
- Exercise 2: Managing State
- Exercise 3: Rendered Lists
- Exercise 4: Controlled Inputs
- Exercise 5: Updating State
Exercise 1: Project Structure#
- Create a new folder within
src/calledcomponents/ - Change your
App.jstoApp.jsx. This will enable more VSCode emmet abbreviations for the file. - Change the contents of your
App.jsxto be a class with a render method that returns<div><h1>Hello World</h1></div>
Exercise 2: Managing State#
Manage a todo list and controlled input with state.
- Create a constructor for the
Appclass - In the body of the constructor, assign an object to a property called
state - On the state object, add two properties:
listtext
- Assign
listas the following array:["ready", "set", "GO"] - Assign
textas an empty string
Exercise 3: Rendering Lists#
A rendered list in React is an array of JSX that we return to the ReactDOM. Render the state list as a React rendered list.
- Update the return statement of the
rendermethod to have anulunderneath theh1. - As a child of the
ul, callthis.state.list.mapwithin a JSX expression - The callback function passed into the map method should return a
lielement - For each string in
this.state.listthat you map over, use the string value as the text content of the returnedli - Lastly, add a
keyprop to the returnedlithat is set to the value of the element's index inthis.state.list
Exercise 4: Controlled Inputs#
A controlled input in React is an input where it's value attribute is set to a state value, and when the value of the input changes, the component state changes. Create a controlled input so that this.state.text is bound to the input.
- Update the return statement of the
rendermethod to have aninputabove theul. - Add a
valueprop to theinputthat is set tothis.state.text - Add an
onChangeprop to theinputthat is set to a function that will:- Receive the HTMLChangeEvent as a parameter
- Call
this.setState()to updatethis.state.texttoe.target.value(the value that is in the input)
Exercise 5: Updating State#
Add functionality to add a new item to the todo list.
- Wrap the
inputelement in aformelement - Add a
buttonelement below theinputelement at the bottom of theformelement- Add a
typeattribute of "submit" - Add text content of "Add"
- Add a
- Create a method named
onSubmiton theAppclass that will:- Receive the HTMLSubmitEvent as a parameter
- Call the
preventDefault()method on the parameter event - Call
this.setState()to update thethis.state.listvalue to a new array contains all of the previous list items and the currentthis.state.text
- Add an
onSubmitprop to theformelement that is set to the methodthis.onSubmit - In the constructor, bind
thisto thethis.onSubmitmethodthis.onSubmit = this.onSubmit.bind(this);
At this point, you should be able to type a new task in the input element, click add, and see the new list item displayed in the unordered list on the screen!
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: