-
Notifications
You must be signed in to change notification settings - Fork 10
Lesson 1.3
In HTML, attributes define the properties of the element. Because JSX is an XML markup language, JSX elements can have attributes too. Attributes that you write in JSX elements are passed to the component represented by the element as properties, or props for short.
So props are the arguments that you pass into a component from a parent component. With JSX, the attributes that you write become properties inside the props object of the resulting component instance.
- Prop’s value can be any type of JavaScript data or expression
- Props are read-only
Once data has been passed to a component using props that data is treated as immutable. This means that although a component receives props, once those props are values inside the component, the component cannot change them.
Each time a JavaScript function is run, the variables inside it are initialized. Because functional components are merely JavaScript functions, it’s not possible for them to have persistent local variables.
If all you want to do is render a static component that never changes, all you need is props. However, the real value of React is in how it enables interactive web applications and manages updates to components in response to user input.
The reason for this is that React only re-renders components in response to state changes. Props are the mechanism for updating components according to state changes.
React uses hooks for functional components giving them additional functionality. The hook that allows you to persist data is useState.
import { useState } from ‘react’
The useState hook accepts an initial value and returns an array containing a stateful variable and a function (setter function) for updating the stateful variable. The setter function needs to be used to update state because it will trigger a re-render the component tree the state variable is defined within.
// [state variable, setter function] = initial value
const [todos, setTodos] = useState([{name: “do homework”}]);
- Inside
/srcdirectory, create a new file calledTodoListItem.jsx - Open
/src/TodoListItem.jsx - Create a new functional React component (see below)
- Declare a function named
TodoListItem - Export
TodoListItemfunction as default module
- Declare a function named
- Add a multi-line return statement to your
TodoListItemfunction (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Move list item JSX from
TodoList.jsxtoTodoListItem.jsx(see below)- Open
/src/TodoList.jsx - Cut (copy and remove) the list item element (
<li>) - Open
/src/TodoListItem.jsx - Inside the multi-line return, paste the list item element (
<li>) - Remove the
keyattribute
- Open
- Refactor
TodoList.jsxto use newTodoListItemcomponent (see below)- Open
/src/TodoList.jsx - Import
TodoListItem - Inside the
map()method, use theTodoListItemcomponent- Pass
keyas a prop equal to theidof thetodoobject - Pass
todoas a prop
- Pass
- Open
- Update
TodoListItemcomponent to use props (see below)- Open
/src/TodoListItem.jsx - Add
propsas a parameter in theTodoListItemfunction - Update the
todoobject reference to come fromprops
- Open
- Run your application and view in browser
- Verify that your Todo List still appears correctly
- Open
/src/AddTodoForm.jsx - Add a
nameattribute to the text input with valuetitle - Inside the
AddTodoFormfunctional component, above thereturnstatement, create a new function calledhandleAddTodothat takeseventas a parameter- First, inside this function, prevent the default behavior of the form submit
- hint:
preventDefaultmethod
- hint:
- Next, retrieve the value of the
titleelement from the event target and store it in a variable namedtodoTitle - Log the value of
todoTitlein 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
onSubmitprop to form element and pass thehandleAddTodofunction 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.jsx - Inside the
Appfunctional component, above thereturnstatement, create a new state variable namednewTodowith update function namedsetNewTodo- hint:
useStatehook
- hint:
- Below the
<AddTodoForm />component, add a paragraph element that displays the value ofnewTodovariable - Pass
setNewTodoas a callback handler prop namedonAddTodoto theAddTodoFormcomponent - Open
/src/AddTodoForm.jsx - Add
propsas a parameter in theAddTodoFormfunction - Inside the
handleAddTodofunction, invoke theonAddTodocallback prop and passtodoTitleas 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