React Fundamentals

What React really does when a component renders, when state changes and when an effect runs. Every lesson runs a real React component and shows its state, its renders and the page at each step.

React · beginner · 21 lessons · 2 h 29 min · free

What you'll learn

Before you start

Lessons

Components and JSX

Functions that return JSX, props, children, conditions and lists.

  1. JSX: Markup Inside JavaScript: A component is a function that returns JSX. See plain JavaScript values flow into the markup through curly braces.
  2. Components and Props: Use one component twice with different props. Follow React as it calls the same function once for each use.
  3. Composition with children: Whatever you put between a component's tags arrives as the children prop. Build a Card that can wrap any content.
  4. Conditional Rendering: Show different things depending on state, with a ternary, with &&, and with an early return. Click Log in and watch the page change.
  5. Rendering Lists with Keys: Filter an array, map it to <li> elements and give each one a key. Watch filter and map run once per item.

State and Events

useState, event handlers, state snapshots and updating objects and arrays.

  1. useState: What Happens on a Click: Follow a counter button through its first render, a click, and the re-render that puts the new number on screen.
  2. Handling Events: Pass a function to onClick and read the event React gives it. One handler serves two buttons.
  3. State Is a Snapshot: Call setScore(score + 1) three times in one click. Why does the score only go up by one?
  4. Update Functions: Pass setScore a function instead of a value, and three updates really add three.
  5. Updating Objects in State: Make a new object with ...spread instead of changing the old one. The bug version shows why changing it in place leaves the screen stuck.
  6. Updating Arrays in State: Add to and change a todo list without mutating it: [...array, item] to add, map to change one item.
  7. Lifting State Up: Two panels that must change together share state kept in their parent. Opening one closes the other.
  8. Forms and Controlled Inputs: Keep an input's text in state, enable the button only for a valid email, and handle the submit.

Effects and Refs

Running code after render, dependencies, cleanup, loading data and refs.

  1. useEffect: Code That Runs After Render: Sync the browser tab title with a counter. See exactly when React runs an effect: after the page has been updated.
  2. Effect Dependencies: An effect with [query] runs only when query changes. Type in the box, then change the theme, and compare.
  3. Effect Cleanup: A clock starts a timer in an effect and stops it in the cleanup. Watch it tick, then hide the clock.
  4. Loading Data in an Effect: Load a user when the component appears and again when the id changes, and ignore answers that arrive too late.
  5. useRef: Values That Don't Re-render: Count clicks in a ref without re-rendering, then show the count and focus an input through a DOM ref.

Sharing State

Context, reducers and custom hooks.

  1. useContext: Passing Data Deeply: Share a theme with every button inside a Provider, without passing props through Toolbar.
  2. useReducer: State Logic in One Place: A cart whose changes all go through one reducer function. Dispatch actions and watch React run the reducer.
  3. Custom Hooks: Wrap useState in your own useToggle hook and use it twice. Each call gets its own state.

Related courses