-
Notifications
You must be signed in to change notification settings - Fork 23
Lesson 1.3
Mary Alice Moore edited this page Feb 18, 2022
·
1 revision
This lesson will teach you the following:
- React Props
- React State
- Handler Function
- Callback handlers
- Inside
/src
directory, create a new file calledTodoListItem.js
- Open
/src/TodoListItem.js
- Create a new functional React component (see below)
- Import
React
from "react" npm package - Declare a function named
TodoListItem
- Export
TodoListItem
function as default module
- Import
- Add a multi-line return statement to your
TodoListItem
function (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Move list item JSX from
TodoList.js
toTodoListItem.js
(see below)- Open
/src/TodoList.js
- Cut (copy and remove) the list item element (
<li>
) - Open
/src/TodoListItem.js
- Inside the multi-line return, paste the list item element (
<li>
) - Remove the
key
attribute
- Open
- Refactor
TodoList.js
to use newTodoListItem
component (see below)- Open
/src/TodoList.js
- Below
React
, importTodoListItem
- Inside the
map()
method, use theTodoListItem
component- Pass
key
as a prop equal to theid
of thetodo
object - Pass
todo
as a prop
- Pass
- Open
- Update
TodoListItem
component to use props (see below)- Open
/src/TodoListItem.js
- Add
props
as a parameter in theTodoListItem
function - Update the
todo
object reference to come fromprops
- Open
- Run your application and view in browser
- Verify that your Todo List still appears correctly
- Open
/src/AddTodoForm.js
- Add a
name
attribute to the text input with valuetitle
- Inside the
AddTodoForm
functional component, above thereturn
statement, create a new function calledhandleAddTodo
that takesevent
as a parameter- First, inside this function, prevent the default behavior of the form submit
- hint:
preventDefault
method
- hint:
- Next, retrieve the value of the
title
element from the event target and store it in a variable namedtodoTitle
- Log the value of
todoTitle
in the console - Finally, reset the form so the text input value is cleared
- First, inside this function, prevent the default behavior of the form submit
- Add
onSubmit
prop to form element and pass thehandleAddTodo
function by reference - View your application in browser
- Enter a value in the text input and submit the form
- Verify that the value you entered is visible in the console
- Verify that form is cleared properly
- Open
/src/App.js
- Inside the
App
functional component, above thereturn
statement, create a new state variable namednewTodo
with update function namedsetNewTodo
- hint:
useState
hook
- hint:
- Below the
<AddTodoForm />
component, add a paragraph element that displays the value ofnewTodo
variable - Pass
setNewTodo
as a callback handler prop namedonAddTodo
to theAddTodoForm
component - Open
/src/AddTodoForm.js
- Add
props
as a parameter in theAddTodoForm
function - Inside the
handleAddTodo
function, invoke theonAddTodo
callback prop and passnewTodo
as an argument - View your application in browser
- Enter a value in the text input and submit the form
- Verify that the value you entered is visible beneath the form