Relire is the French reader I built for my own TCF Canada preparation. It runs in the browser with no account, and learning data stays on the device. On November 19, I spent the day moving reader state out of effects. The first commit replaced 16 effects across 16 files. Two more commits the same day applied the same idea to the rest of the reader.
Three jobs, one mechanism
The 16 effects did three different jobs. Some touched DOM nodes: focus, page lock, dock height. Some subscribed to things outside React: the install prompt, voices, unit reads. One waited on a promise: the verb retry. All three went through useEffect, so each carried its own guards: a mounted flag, a cleanup function, a check against stale reads.
I replaced each effect with the mechanism that fit its job.
A DOM node is its own entry point, so focus, page lock, and dock height became ref callbacks. There is nothing to subscribe to and nothing to clean up. The node arrives, and the callback runs.
Subscriptions to things outside React became an external store. I added a small shared helper, liveRead, that wraps an async read as a store for useSyncExternalStore. It reads when the first component subscribes. It reads again when the source announces a change. It drops the state when the last subscriber leaves. A token counts requests, so only the newest read counts. A late reply never overwrites a newer one. Nothing lands after unmount.
const load = () => {
const request = ++token;
void read().then((next) => {
if (request !== token) return;
state = next;
for (const listener of listeners) listener();
});
};
The verb retry became use(). Before, the section kept the retried result in state and fetched it in an effect with a live flag to guard the setState. Now it keeps one retry promise per failed result in a WeakMap, so a re-render reads the same promise. Suspense shows the old lines while the retry runs.
const retries = new WeakMap<LexiconVerbLines, Promise<LexiconVerbLines>>();
function Retried({ lookup, lexicon }) {
let retry = retries.get(lexicon);
if (!retry) {
retry = lexiconVerbLines(lookup);
retries.set(lexicon, retry);
}
return <VerbFormLines lookup={lookup} settled={use(retry)} />;
}
The commit message puts it in one line. “Effects without another entry point stay.” This was not a purge. Where no better entry point existed, the effect stayed.
The same idea, twice more
The same day brought two more commits in the same direction. Reader progress, studied topics, and the shadowing log moved to stores (+182/-155, 9 files). Bundled grammar and lexicon data moved to use() (+467/-329, 21 files). The three refactor commits together came to +869/-683 lines.
The day also shipped user-visible fixes next to the refactor. An Anki import fix brings in every note of a deck. An imported deck opens for study right away. The refactor did not sit alone. It shared the day with the fixes it made room for.
The rule I kept
Pick the mechanism by asking where the read starts. A DOM node means a ref callback. Something outside React means an external store. A promise means use(). An effect is the fallback when none of those fit, not the default. That rule removed 16 effects in one day.
