Session Plan
Forms / Event Handling
DOM Event Handlers in React
Setting event handlers directly onto elements (e.g.
onClickinstead ofelement.addEventListener("click", …))Overview of event handlers exposed by React
Managing Forms
Adding a form and adding inputs to the form
Submitting forms with
onSubmitPerforming input validation in JavaScript (in addition to HTML input validation, and server-side input validation)
Hooks
Introduction to Hooks
You already know one hook! ->
useStateRules of Hooks (don't wrap in
ifstatements, use dependency array)Hooks run on every render, unless they have dependencies
Hooks run when dependencies change
How to use useEffect
useEffectUsage of
useEffectDependencies of
useEffectUse
useEffectfor synchronization (with external systems)Returning a cleanup function from
useEffect
Loading Data
Connecting to APIs
Calling async APIs using
fetch, withinuseEffectImplementing initial data loading, showing a spinner
Exercises
Handling User Input & Events
1. Create a ClickCounter component
Create a new component called
ClickCounterUse the
useStatehook to manage acountstate variableImplement an
onClickevent handler function that increments thecountRender a button and display the current value of
count
2. Build a SimpleForm component
Create a new component called
SimpleFormUse the
useStatehook to manage the state of form inputsCreate input fields for the form (e.g., name, email, message)
Implement a
handleSubmitfunction to handle form submissionRender the form inputs and a submit button
3. Develop a TodoList component
Create a new component called
TodoListUse the
useStatehook to manage an array of todo itemsImplement a function to add a new todo item to the array
Implement a function to remove a todo item from the array
Render a list of todo items using the
.mapmethod, ensuring each item has a uniquekeypropRender an input field to add new todos and buttons/functionality to remove todos
Practice Using useEffect
useEffect4. Create a Clock component
Create a new component called
ClockUse the
useStatehook to manage the current timeUse the
useEffecthook to set up anIntervalthat updates the time every secondImplement the cleanup function in
useEffectto clear the interval when the component unmountsLet the component render the current time
5. Build a DataFetcher component
Create a new component called
DataFetcherUse the
useStatehook to manage the data, loading, and error statesUse the
useEffecthook to fetch data from a public API when the component mountsImplement loading and error states in the component's JSX
Render the fetched data when it's available
6. Develop a WindowResizer component
Create a new component called
WindowResizerUse the
useStatehook to manage the window sizeUse the
useEffecthook to set up an event listener for theresizeeventUpdate the window size state whenever the
resizeevent is triggeredImplement the cleanup function in
useEffectto remove the event listener when the component unmountsRender the current window size in the component
Last updated