Context with a reducer, from the intermediate course, keeps shared state
inside React: a provider component owns it. Sometimes state needs to live
outside any component:
code that isn't React needs it too, such as a module that saves changes to a
server or a timer that keeps running;
several React roots, or a page that's partly React, share it;
many components each need a different small part of it, and shouldn't all
re-render when any part changes.
An external store is one way to meet those needs. Libraries such as Redux and
Zustand offer store integrations built around React's subscription APIs;
other libraries use different designs. Here you'll build a small store and
connect it with useSyncExternalStore.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Press Log 25 minutes twice and watch the console.
The store
createStore returns three functions:
getState() returns the current state.
setState(update) replaces it, with a value or an updater function, then
calls every listener. If nothing changed, it does nothing.
subscribe(listener) adds a listener, and returns a function that removes it:
JavaScript
export function createStore(initialState) { let state = initialState; const listeners = new Set(); return { getState: () => state, setState(update) { const next = typeof update === "function" ? update(state) : update; if (Object.is(next, state)) return; state = next; listeners.forEach((listener) => listener()); }, subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); }, };}
It's ordinary JavaScript. session.js creates one store and two functions that
change it. autoSave.js subscribes without any React, and logs saved:
… each time, as a real app might save to a server:
export function useStore(store) { return useSyncExternalStore(store.subscribe, store.getState);}
React calls subscribe when the component mounts, with a callback that
re-renders the component, and calls the function it returns when the component
unmounts. Each render reads the current value with getState.
You could do this with useState and useEffect, but useSyncExternalStore
also handles the hard cases: a store that changes during a render, or during a
transition. With it, every component on the screen sees the same value at the
same time.
Predict
LogButtons doesn't call useStore. Does it re-render when you log a session?
Check your prediction
No. Only components that call useStore subscribe. LogButtons just calls
logSession, a plain function, so it never needs to re-render. With context,
every consumer re-renders for any change; a store lets each component choose.
The next lesson takes that further.
Exercise
Reset from outside React
Add a resetDay() function to session.js that sets minutes and sessions back
to 0. Call it from autoSave.js when the total passes 100 minutes, logging
daily limit reached, starting over. The header should update by itself.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Code outside React changes the store the same way React code does: through the
store's own update function. Every subscriber, including the components
reading it through useSyncExternalStore, hears about the change, so nothing
in the header needs touching. Watch out for the store's other fields.
Hint 2: a first step
Write resetDay first, then call it from the subscribe callback in
autoSave.js, after the log line, behind a check on the total. Log 50 minutes
three times. Setting state from a listener runs the listeners again, so you'll
see a second saved: line straight after the reset. Why doesn't your check
make it loop forever?