10LOC

#external-store

Reactadvanced

useSyncExternalStore for tearing-free external store subscriptions

import { useSyncExternalStore } from "react";

const EVENTS = ["online", "offline"] as const;

const subscribe = (onStoreChange: () => void) => {

Subscribe a component to state that lives outside React, like navigator.onLine, without ever showing a torn snapshot mid-render.