UNPKG

@mongez/react-atom

Version:

A simple state management tool for React Js.

24 lines (23 loc) 926 B
import {jsx}from'react/jsx-runtime';import {atomsList}from'@mongez/atom';import {createContext,useContext,useState}from'react';const AtomContext = createContext({}); /** * Get clone of the given atom key */ function useAtom(key) { const context = useContext(AtomContext); return context[key]; } function AtomProvider({ register = atomsList(), defaultValue, children, }) { const [currentAtoms] = useState(() => { const atoms = {}; for (const atom of register) { const newAtom = atom.clone(); atoms[atom.key] = newAtom; if (defaultValue?.[atom.key]) { newAtom.silentUpdate(defaultValue[atom.key]); } } return atoms; }); console.log(currentAtoms); return (jsx(AtomContext.Provider, { value: currentAtoms, children: children })); }export{AtomContext,AtomProvider,useAtom};//# sourceMappingURL=context.js.map