State and forms

useState and updater functions, controlled inputs and validation, and lifting state up to share it.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    State: a component's memory Intermediate

    useState for values that change over time, why setting state makes React render again, state as a snapshot, updater functions, and keeping objects and arrays in state without changing them.

  2. 2

    Forms and controlled inputs Intermediate

    Controlled inputs that show state and update it on every keystroke, text fields, checkboxes and selects, handling submit, validating, and computing values from form state instead of storing them.

  3. 3

    Sharing state between components Intermediate

    Lifting state up to the closest common parent, passing values and setter callbacks down as props, keeping one source of truth, and choosing where each piece of state should live.

Module practice

Module quiz

1. What does calling a state setter do?
2. Which attribute makes a checkbox controlled?
3. count is 0. After setCount(c => c + 1) twice, what is it?
4. How do you remove an item from an array in state?
5. Two siblings need the same state. Where should it live?
6. A form has a price and a quantity. Why not store the total in state too?

‹ Rendering data and respondingEffects, refs and custom Hooks ›