Session Plan
Props
Building Components & Using Props
What are props and why are they used?
Props as a way to make components flexible and re-usable
Why prop values should never be changed within components (one-way data flow)
How to pass props to components
Reading props using
props.xsyntaxReading props using destructing syntax
Forwarding props using spread syntax
Using Children Props
What are children props?
Creating wrapper components
Conditional Rendering Based on Props
Conditional rendering using if/else
Conditional rendering using ternary operator
(<condition> ? <then> : <else>)Conditional rendering using a boolean expression
<condition> && <then>
State & Conditional Rendering
Introduction to State
What is State and when to use it
Differences between Props and State
useState Hook Syntax
useState Hook SyntaxIntroduction to React hooks
How to declare and use State with
useState
Updating State
Updating State consisting of arrays
Updating State consisting of an object
React Rendering Triggers
What causes React to re-render components
Using an update function for multiple State changes following after each other
Dynamically Rendering Content Based on State
Conditional rendering using if/else
Conditional rendering using ternary operator
(<condition> ? <then> : <else>)Conditional rendering using a boolean expression
<condition> && <then>
Rendering Many Components
Rendering multiple items using
.map()
Sharing State Between Components
Lifting state up to the parent
Exercises
Exercises Part 1
1. Create a Re-usable Button Component
Button ComponentCreate a new component called
ButtonAccept
textandonClickas propsRender a
<button>element with thetextprop as its contentAttach the
onClickprop to the button'sonClickevent handler
2. Create a Card Component
Create a new component called
CardAccept
title,description, andimageUrlas propsUse destructuring to extract the props
Render a card-like structure with the provided
title,description, andimageUrl
3. Create a Layout Component
Create a new component called
LayoutAccept
childrenas a propRender a layout structure (e.g.
header,maincontent area,footer) with thechildrenprop rendered inside the main content areaPass children by adding them in between the tags of the parent component
4. Implement Conditional Rendering
Create a new component called
ToggleContentAccept a
showprop as a boolean valueAccept
contentas a prop, which can be a string or a React elementRender the
contentprop only if theshowprop is true, otherwise render a message
Exercises Part 2
5. Create a Counter Component
Create a new component called
CounterUse the
useStatehook to manage a count state variableImplement functions to increment (count up) and decrement (count down) the count
Render the current count value and buttons to call the increment and decrement functions
6. Develop a Form Component
Create a new component called
FormUse the
useStatehook to manage the state of form input fieldsImplement a function to handle form submission, you may want to use
onSubmitin combination with a<button type="submit">Render a form, input fields for each form field and the mentioned submit button
7. Create a ParentChild Component Set
Create a new component called
ParentCreate another component called
ChildIn the
Parentcomponent, use theuseStatehook to manage a state variableImplement a function to update the state variable
Render the
Childcomponent and pass the state variable and the update function as propsIn the
Childcomponent, render the data received from the parent and a button to call the update function that changes the state
Last updated