generated from Code-the-Dream-School/ctd-react
-
Notifications
You must be signed in to change notification settings - Fork 32
Lesson 1.4: Advanced React Props and State
E Thompson edited this page Sep 25, 2021
·
2 revisions
This lesson will teach you the following:
- Lifting State
- Controlled Components
- Props handling
- Open
/src/App.js
- Create new state variable named
todoList
with settersetTodoList
and default value of an empty Array - Pass
todoList
state as a prop namedtodoList
to theTodoList
component - Open
/src/TodoList.js
- Add
props
as a parameter to theTodoList
functional component - Change
todoList
to reference props instead of the hard-coded variable - Delete the hard-coded
todoList
variable - Run your application and view in browser
- Verify that your Todo List is now empty (no list items)
- Open
/src/AddTodoForm.js
- Create new state variable named
todoTitle
with settersetTodoTitle
- Modify the
<input>
element to be a controlled input- Add
value
prop equal totodoTitle
from component props - Add
onChange
prop equal tohandleTitleChange
function reference (we will declare this function in the next step)
- Add
- Above the
handleAddTodo
function, declare a new function namedhandleTitleChange
that takesevent
as a parameter- First, retrieve the input value from the
event
object and store in variable namednewTodoTitle
- Then, call the state setter
setTodoTitle
and passnewTodoTitle
- First, retrieve the input value from the
- In the
handleAddTodo
function, remove thetodoTitle
variable and updateonAddTodo
callback handler to pass ourtodoTitle
state variable instead - Run your application and view in browser
- Enter a new todo in "Add Todo" form, submit, and verify that the title appears below
- Open
/src/App.js
- Remove the
newTodo
state variable and the corresponding JSX that displays it - Declare a new function named
addTodo
that takesnewTodo
as a parameter- Call the
setTodoList
state setter and use the spread operator to pass the existing Objects in thetodoList
Array along with thenewTodo
Object
- Call the
- Change the value of the
onAddTodo
prop forAddTodoForm
toaddTodo
- Open
/src/AddTodoForm.js
- Inside
handleAddTodo
, update theonAddTodo
callback prop to pass an Object instead of a String; Object should have the following properties:-
title
: equal totodoTitle
-
id
: unique identifier (hint: useDate.now()
to generate a unique number)- Disclaimer: we are suggesting
Date.now()
for now as a placeholder for unique number generation, but in the future you should not use this
- Disclaimer: we are suggesting
-
- Inside
handleAddTodo
, remove thereset()
method and replace it with logic to reset thetodoTitle
state to an empty String - Run your application and view in browser
- Enter a todo in "Add Todo" form, submit, and verify item is visible in todo list
- Enter another todo, submit, and verify that two items are visible in todo list
- Open
/src/TodoList.js
and updateprops
to use destructuring - Open
/src/TodoListItem.js
and updateprops
to use destructuring - Open
/src/AddTodoForm.js
and updateprops
to use destructuring
Created by Code the Dream