Module 4 of 7

Advanced React Patterns

These notes are about designing component and hook APIs: how much control a component hands to whoever uses it, and how its pieces find each other without being wired up by hand. Each pattern answers that question differently:

The last four build one useToggle hook for the store's "Gift wrap" switch, handing over a little more control each time.

Every log, render list and DOM attribute here was recorded by running the product store in Chrome (examples/product-store/src/lessons/patterns).

These patterns come from component libraries you probably use: Radix and Reach (compound components), React Aria (slots), Downshift (prop getters, state reducer, control props). Recognizing them makes those libraries' APIs much easier to read.

Start with “Composition and Layout Components”All notesInterview questions

The notes

Read them in order. Each one builds on the ones before it.

02Good to know

The Latest Ref Pattern

A function built once that still calls the newest callback: debounce done right, and useEffectEvent.

Read
03Must know

Compound Components

Toggle, ToggleButton, ToggleOn and ToggleOff sharing state through context, like select and option.

Read
04Good to know

The Slots Pattern

A root component publishes props per slot name; generic Label, Input and Text pieces pick theirs up.

Read
06Good to know

State Initializers

Let the consumer choose the starting state, and make reset go back to it, even if the prop changes later.

Read
07Must know

The State Reducer Pattern

Let the consumer decide what each action does, by passing their own reducer that can veto or delegate.

Read
08Must know

Control Props

A hook that works on its own, or lets the parent own the value: suggestions through onChange, like input value.

Read