Build Jotai-style atoms: derived atoms with dynamic dependencies, batched writes, mounting and React hooks

hardAbout 65 minutesUpdated CodingProreactstate-managementjotaiatomsderived-stateusesyncexternalstore
Not solved

Share this question

Problem

Jotai and Recoil model state as atoms: small units of state, plus derived atoms computed from others. Components read exactly the atoms they need and re-render only when those change. Interviewers like it because a correct store has to solve the classic reactive problems: dependencies that change between runs, diamonds that must not show half-updated values, and work that should stop when nobody is watching. Build the core of Jotai and its React hooks.

const price = atom(100);
const qty = atom(2);
const total = atom((get) => get(price) * get(qty));
const store = createStore();
store.sub(total, () => console.log(store.get(total)));
store.set(qty, 3); // logs 300

Ref. UR-1438 · © UIRound · uiround.com/questions/build-jotai-style-atoms-derived-atoms-with-dynamic-dependencies-batched-writes-mounting-and-react-hooks

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment