Exercise: React State and Props
#
ObjectiveYour 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 StartedThis is part 2 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 1.
#
StepsReact 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.js
toApp.jsx
. This will enable more VSCode emmet abbreviations for the file. - Change the contents of your
App.jsx
to be a class with a render method that returns<div><h1>Hello World</h1></div>
#
Exercise 2: Managing StateManage a todo list and controlled input with state.
- Create a constructor for the
App
class - In the body of the constructor, assign an object to a property called
state
- On the state object, add two properties:
list
text
- Assign
list
as the following array:["ready", "set", "GO"]
- Assign
text
as an empty string
#
Exercise 3: Rendering ListsA 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
render
method to have anul
underneath theh1
. - As a child of the
ul
, callthis.state.list.map
within a JSX expression - The callback function passed into the map method should return a
li
element - For each string in
this.state.list
that you map over, use the string value as the text content of the returnedli
- Lastly, add a
key
prop to the returnedli
that is set to the value of the element's index inthis.state.list
#
Exercise 4: Controlled InputsA 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
render
method to have aninput
above theul
. - Add a
value
prop to theinput
that is set tothis.state.text
- Add an
onChange
prop to theinput
that is set to a function that will:- Receive the HTMLChangeEvent as a parameter
- Call
this.setState()
to updatethis.state.text
toe.target.value
(the value that is in the input)
#
Exercise 5: Updating StateAdd functionality to add a new item to the todo list.
- Wrap the
input
element in aform
element - Add a
button
element below theinput
element at the bottom of theform
element- Add a
type
attribute of "submit" - Add text content of "Add"
- Add a
- Create a method named
onSubmit
on theApp
class that will:- Receive the HTMLSubmitEvent as a parameter
- Call the
preventDefault()
method on the parameter event - Call
this.setState()
to update thethis.state.list
value to a new array contains all of the previous list items and the currentthis.state.text
- Add an
onSubmit
prop to theform
element that is set to the methodthis.onSubmit
- In the constructor, bind
this
to thethis.onSubmit
methodthis.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 LinksIf you feel stuck, or would like to see the finished code for this exercise to check your work, check out: