Subscribe with useSyncExternalStore
Read a value owned outside React
An external store holds changing information independently of component state. An inventory board shared with another widget is one example. useSyncExternalStore connects a component to the store through a subscription and a snapshot reader. Use ordinary state for local values that React already owns. The examples below are independent App.jsx files; visible outcomes are predictions until a real React browser test is run. The store examples require React 18 or newer.
import React,{useState,useEffect,useSyncExternalStore} from 'react';function createBoard(initial=0) { let snapshot=Object.freeze({available:initial}); const listeners=new Set(); return { getSnapshot:()=>snapshot, subscribe(callback){ const entry=()=>callback(); listeners.add(entry); return ()=>listeners.delete(entry); }, setAvailable(value){ if(!Number.isSafeInteger(value)||value<0) throw new RangeError('Invalid availability'); if(value===snapshot.available)return; snapshot=Object.freeze({available:value}); for(const listener of [...listeners])listener(); }, listenerCount:()=>listeners.size };}const board=createBoard(3);function Count(){const data=useSyncExternalStore(board.subscribe,board.getSnapshot);return <p>Available kits: {data.available}</p>;}export default function App(){return <section><Count/><Count/><button onClick={()=>board.setAvailable(Math.max(0,board.getSnapshot().available-1))}>Borrow kit</button></section>;}Both readers should initially display 3. Borrowing one kit changes both to 2. React registers a callback and can read the store again after a notification; the callback does not need to carry the data. A stable subscribe function avoids unnecessary re-registration. The singleton is only a client-side fixture, not a shared server store. Its shallow freeze is sufficient because the snapshot contains only one primitive.
Keep snapshot identity stable
import React,{useState,useEffect,useSyncExternalStore} from 'react';function createBoard(initial=0) { let snapshot=Object.freeze({available:initial}); const listeners=new Set(); return { getSnapshot:()=>snapshot, subscribe(callback){ const entry=()=>callback(); listeners.add(entry); return ()=>listeners.delete(entry); }, setAvailable(value){ if(!Number.isSafeInteger(value)||value<0) throw new RangeError('Invalid availability'); if(value===snapshot.available)return; snapshot=Object.freeze({available:value}); for(const listener of [...listeners])listener(); }, listenerCount:()=>listeners.size };}const board=createBoard(2);export default function App(){const snapshot=useSyncExternalStore(board.subscribe,board.getSnapshot);return <section><p>{snapshot.available} kits</p><button onClick={()=>board.setAvailable(snapshot.available)}>Keep same count</button><button onClick={()=>board.setAvailable(board.getSnapshot().available+1)}>Return kit</button></section>;}Keep same count leaves both the stored object and its value unchanged. Return kit produces a new object and notifies listeners. Returning a newly allocated object on every read would make React observe continuous changes even when nothing changed. Mutating the existing object would hide meaningful changes. React compares snapshots using Object.is; do not rely on exact render counts. In concurrent rendering React checks external snapshots for consistency before committing a transition and may restart as a blocking update to avoid inconsistent versions on screen (sometimes called tearing).
Bridge a browser-owned value
import React,{useState,useEffect,useSyncExternalStore} from 'react';function subscribe(callback){window.addEventListener('online',callback);window.addEventListener('offline',callback);return ()=>{window.removeEventListener('online',callback);window.removeEventListener('offline',callback);};}function getSnapshot(){return navigator.onLine;}export default function App(){const online=useSyncExternalStore(subscribe,getSnapshot);return <p role="status">Browser network hint: {online?'online':'offline'}</p>;}This client-only example subscribes to two browser events and returns the current boolean. Online does not prove that the application server is reachable; a request can still fail. The cleanup removes the same callbacks. For server rendering, supply a consistent server snapshot or use a client-only boundary. Practice: predict what both board readers display after borrowing three kits and then trying once more.