0Pricing
React Academy · Lesson

Subscribing to Browser APIs

Build hooks that subscribe to matchMedia, navigator.onLine, window history, and localStorage using useSyncExternalStore.

Subscribing to Browser APIs is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Subscribing to matchMedia

Browser media queries are a perfect external store. You can subscribe to a matchMedia query for dark mode or a breakpoint and re-render whenever the match state flips.

The subscribe function adds a change listener to the MediaQueryList, and getSnapshot returns its current matches boolean, giving you reactive access to responsive conditions.

A useMediaQuery Hook

Wrapping that logic into a useMediaQuery hook lets components ask whether a query matches with a single call. The hook builds a stable subscribe and getSnapshot around a given query string.

This produces a clean, reusable primitive for responsive behavior that stays consistent under concurrent rendering thanks to useSyncExternalStore.

navigator.onLine Status

The browser exposes online and offline events plus navigator.onLine. Subscribing to those events and reading the property gives you a live connectivity status inside React.

getSnapshot returns navigator.onLine, and subscribe attaches listeners for the online and offline events so the component updates as connectivity changes.

A useOnlineStatus Hook

Bundling that into a useOnlineStatus hook lets any component show offline banners or disable actions when the network drops. The official React docs use this exact example to demonstrate the hook.

It is a small but illustrative case where an external browser signal maps cleanly to a piece of reactive state.

Subscribing to window.location

You can subscribe to navigation changes by listening for the popstate event and reading window.location in getSnapshot. This underpins simple custom routers built on the History API.

Because pushState does not emit popstate, custom routers often dispatch their own event so the snapshot updates on programmatic navigation too.

localStorage Cross-Tab via the storage Event

The storage event fires in other tabs when localStorage changes, which makes it an external store you can subscribe to. getSnapshot reads the current value, and subscribe listens for storage events.

This enables cross-tab synchronization, where updating a value in one tab reactively updates components in another.

deviceOrientation

The deviceorientation event reports the physical tilt of a device. Subscribing to it and reading the latest angles in getSnapshot turns motion data into reactive state.

This is useful for parallax effects or interactive visualizations that respond to how a phone is held.

An IntersectionObserver Wrapper

IntersectionObserver tells you when an element enters or leaves the viewport. You can wrap it so subscribe creates the observer and getSnapshot returns the latest isIntersecting value.

This gives a reactive isVisible flag for lazy loading images, triggering animations, or implementing infinite scroll sentinels.

ResizeObserver for Element Size

ResizeObserver reports element size changes. Wrapping it lets subscribe observe an element and getSnapshot return its current dimensions, so components react to layout changes precisely.

This is more efficient and accurate than listening to window resize when you care about a specific element rather than the whole viewport.

Composing into useWindowSize

You can combine the resize event with the hook to build a useWindowSize that returns the current width and height. subscribe attaches a resize listener and getSnapshot reads window dimensions.

Remember to return a stable snapshot, caching the size object so unchanged dimensions do not cause re-renders, keeping the composition efficient.

Quick Check: useMediaQuery Implementation

Reason about how a useMediaQuery hook uses the store hook.

Recap: Subscribing to Browser APIs

Many browser APIs are external stores you can bridge with useSyncExternalStore: matchMedia for breakpoints, navigator.onLine for connectivity, location and storage for navigation and cross-tab sync, plus orientation, intersection, and resize observers.

Each follows the same shape, a subscribe that attaches and removes listeners and a stable getSnapshot. Compose them into hooks like useMediaQuery, useOnlineStatus, and useWindowSize.

Frequently asked questions

Is the “Subscribing to Browser APIs” lesson free?

Yes — the full text of “Subscribing to Browser APIs” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Subscribing to Browser APIs”?

Build hooks that subscribe to matchMedia, navigator.onLine, window history, and localStorage using useSyncExternalStore. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Subscribing to Browser APIs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Problem with External Store Subscriptions
  2. useSyncExternalStore API and Parameters
  3. Subscribing to Browser APIs
  4. Building a Custom Store with useSyncExternalStore
← Back to React Academy