This library provides a custom implementation of Jotai for legacy React and Node.js environments. It offers atom creation, state management, and subscription capabilities, ensuring compatibility with older versions of React.
Tested with react 16 and nodejs10
You can install the library via npm:
npm install jotai-legacy
Or via yarn:
yarn add jotai-legacy
You can create an atom with an initial value or a function to compute the initial value:
import { atom, useAtom, useAtomValue } from 'jotai-legacy';
const countAtom = atom(0);
const countComputedAtom = atom((get) => get(countAtom) * 2);
Use the useAtom
hook to get the current value of an atom and a setter function to update it:
const [count, setCount] = useAtom(countAtom);
const increment = () => setCount((prev) => prev + 1);
Use the useAtomValue
hook to get the current value of an atom without subscribing to updates:
const count = useAtomValue(countAtom);
Creates an atom with the given initial value.
initialValue
(any | function): The initial value of the atom or a function to compute the initial value.- Returns: An atom object with
get
,set
,subscribe
, and_subscribers
methods.
Custom hook to use an atom.
atom
(Atom): The atom to use.- Returns: A tuple with the current atom value and a setter function.
Custom hook to get the value of an atom.
atom
(Atom): The atom to get the value from.- Returns: The current value of the atom.
Custom hook that mimics the behavior of React's useSyncExternalStore
.
subscribe
(function): The function to subscribe to store changes.getSnapshot
(function): The function to get the current state snapshot.- Returns: The current state.
We welcome contributions to this project. Please follow the guidelines below to contribute:
- Fork the repository.
- Create a new branch with a descriptive name.
- Make your changes and commit them with clear messages.
- Open a pull request with a detailed description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
This library is developed and maintained by masrurimz.