UNPKG

@lucianojd/recoil-sync-next

Version:

recoil-sync stores for Next.js

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