This assignment will teach you the following:
- Lifting State
- Controlled Components
- Props handling
Merge your pull request from the previous lesson (if you haven't already):
Checkout your main branch and pull changes:
git checkout main
git pull
Create a new local branch to work on separate from the main
branch:
git checkout -b lesson-1-4
Now, open the project directory in your code editor and continue to the next section.
- 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
Step 1: On page load, list is empty
Step 3: After submitting new todo
Step 4: After submitting another new todo
Check the status of your local repository to double-check the changes you made:
git status
Stage the file(s) that you edited:
git add .
Check the status again and notice that the changes from before are now staged:
git status
Create a commit for the changes you made and add a message describing the changes you made:
Note: Replace
<message>
with your message
git commit -m "<message>"
Push your commit to the remote repository (visible in GitHub):
git push
Check the log to make sure your commit has been published:
git log --oneline
Create a pull request and submit:
Created by Code the Dream