generated from Code-the-Dream-School/ctd-react
-
Notifications
You must be signed in to change notification settings - Fork 32
Lesson 1.2: React Components
E Thompson edited this page Sep 25, 2021
·
2 revisions
This lesson will teach you the following:
- React DOM
- Component Definition
- Component Instantiation
- React Components
- Inside
/src
directory, create a new file calledTodoList.js
- Open
/src/TodoList.js
- Create a new functional React component (see below)
- Import
React
from "react" npm package - Declare a function named
TodoList
- Export
TodoList
function as default module
- Import
- Add a multi-line return statement to your
TodoList
function (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Move list JSX from
App.js
toTodoList.js
(see below)- Open
/src/App.js
- Cut (copy and remove) the entire list element (
<ul>
) code - Open
/src/TodoList.js
- Inside the multi-line return, paste the list element (
<ul>
) code
- Open
- Move
todoList
array fromApp.js
toTodoList.js
(see below)- Open
/src/App.js
- Cut (copy and remove) the entire
todoList
variable declaration - Open
/src/TodoList.js
- Below the import, paste the
todoList
variable
- Open
- Refactor
App.js
to use newTodoList
component (see below)- Open
/src/App.js
- Below
React
, importTodoList
- Below the level-one heading, use the
TodoList
component
- Open
- Run your application and view in browser
- Verify that your Todo List still appears correctly
- Inside
/src
directory, create a new file calledAddTodoForm.js
- Open
/src/AddTodoForm.js
- Create a new functional React component (see below)
- Import
React
from "react" npm package - Declare a function named
AddTodoForm
- Export
AddTodoForm
function as default module
- Import
- Add a multi-line return statement to your
AddTodoForm
function (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Write JSX for "Add Todo" form (see below)
- Create a
<form>
element - Inside the
<form>
element, create a<label>
element with text "Title" - Next, create a text
<input>
element withid
"todoTitle" - Add
htmlFor
attribute to<label>
element with same value asid
above - Next, create a submit
<button>
element with text "Add"
- Create a
- Use
AddTodoForm
component inApp.js
(see below)- Open
/src/App.js
- Below
React
, importAddTodoForm
- Below the level-one heading, use the
AddTodoForm
component
- Open
- Run your application and view in browser
- Verify that "Add Todo" form appears below heading
Created by Code the Dream