UNPKG

@lucianojd/recoil-sync-next

Version:

recoil-sync stores for Next.js

16 lines (15 loc) 640 B
import { atomFamily, selectorFamily, } from 'recoil'; export function initializableAtomFamily(options) { const baseAtom = atomFamily({ ...options, default: undefined, }); const evaluationSelector = selectorFamily({ key: `${options.key}::withInitialValue`, get: ({ param, initialValue }) => ({ get }) => { var _a; return (_a = get(baseAtom(param))) !== null && _a !== void 0 ? _a : initialValue; }, set: ({ param }) => ({ set }, newValue) => set(baseAtom(param), newValue), }); return (param, initialValue) => { return evaluationSelector({ param, initialValue }); }; }