UNPKG

jotai

Version:

👻 Next gen state management that will spook you

34 lines (26 loc) • 1.1 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jotai')) : typeof define === 'function' && define.amd ? define(['exports', 'jotai'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jotaiZustand = {}, global.jotai)); })(this, (function (exports, jotai) { 'use strict'; function atomWithStore(store) { var baseAtom = jotai.atom(store.getState()); baseAtom.onMount = function (setValue) { var callback = function callback() { setValue(store.getState()); }; var unsub = store.subscribe(callback); callback(); return unsub; }; var derivedAtom = jotai.atom(function (get) { return get(baseAtom); }, function (get, _set, update) { var newState = typeof update === 'function' ? update(get(baseAtom)) : update; store.setState(newState, true); }); return derivedAtom; } exports.atomWithStore = atomWithStore; Object.defineProperty(exports, '__esModule', { value: true }); }));