Your own store isn't the only external store. The browser has plenty: whether
the network is up, the screen width, the system's colour scheme, the page's
visibility. Each has a current value and an event that says it changed, which
is exactly the shape useSyncExternalStore expects.
The subscribe function is defined outside the hook, so it's the same
function on every render. If it were a new function each time, React would
unsubscribe and subscribe again on every render.
To try it, open your browser's developer tools, find Network, and switch to
Offline. The message changes straight away. Switch back to No
throttling.
navigator.onLine reports the browser's idea of network connectivity; it
doesn't prove your server is reachable. Keep handling request failures even
when the label says Online.
Media queries
useMediaQuery follows any CSS media query with window.matchMedia. Change your
system between light and dark mode, or use the developer tools' Rendering
panel to emulate prefers-color-scheme: dark, and the lab changes theme:
Use this for behaviour that JavaScript must decide: which component to render,
how many items to load. For styling alone, plain CSS media queries are simpler.
Predict: does this break the rule?
Look at the subscribe function passed to useSyncExternalStore in
useMediaQuery. The lab logs subscribed to each time React calls it. Press
Log 5 minutes three times. What does the console show?
Check your prediction
subscribed to (prefers-color-scheme: dark) after every click. The
subscribe function is an arrow function written inside the hook, so each
render makes a new one, and React unsubscribes from the old function and
subscribes with the new one. It's exactly what the online example avoided.
Here it costs little: one matchMedia call and one listener per render. In a
component that re-renders on every keystroke, or with a subscription that's
expensive to set up (a socket, a server push), it matters. The exercise fixes
it without losing the query parameter.
Compared with an effect
You could write useOnlineStatus with useState and an effect that adds the
listeners. You can initialize state from navigator.onLine, but you still
need to handle a change between rendering and subscribing.
useSyncExternalStore manages that subscription consistency for you.
These are browser-only labs. Server rendering also needs a getServerSnapshot
argument and a value that matches the first client render; window and
navigator aren't available on the server.
Exercise
Subscribe once per query
Change useMediaQuery so that Log 5 minutes logs nothing: the hook
subscribes once when the component appears, and again only if its query
changes. subscribeOnline could move outside the hook because it needs
nothing from it. This subscribe function needs query, so it can't.
Then use it a second time in App: show Wide layout or Narrow layout
under the theme line with useMediaQuery("(min-width: 600px)"), and check that
the console shows one subscribed to line per query at the start, and none
after clicking.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
React compares the subscribe function with the last render's using Object.is.
You need the same function object on every render, until query changes.
On Intermediate Day 6 you met a hook that does exactly that for functions.
Hint 2: a first step
Give the subscribe function a name inside the hook,
const subscribe = ..., and pass subscribe to useSyncExternalStore.
Nothing changes yet: the click still logs. Now wrap that function so React
keeps it between renders, and decide what goes in its dependency list.
Import useCallback from "react". query is the dependency: a different
query needs a different listener. The snapshot function can stay inline: React
calls it to read the value but doesn't resubscribe when it changes.