About this project
Jotai is a primitive and flexible state management library for React, distributed as an npm package. Its core API is small (about 2kb) and it is designed to scale from a simple useState replacement up to enterprise TypeScript applications.
The central concept is the atom, which represents a piece of state. Primitive atoms are created with an initial value, which may be a string, number, object, or array. Components consume atoms through the useAtom hook, which behaves similarly to React.useState and returns the current value plus a setter.
Derived atoms can be built from existing atoms. A read-only derived atom is created by passing a read function as the first argument; the get helper retrieves the contextual value of any atom. Multiple atoms can be combined into a single derived atom, including through functional patterns such as mapping get over an array of atoms.
Async derived atoms are supported by making the read function async, which integrates with React Suspense for loading states. Writable derived atoms accept a write function as the second argument, where get returns the current value and set updates an atom. Write-only atoms are possible by omitting the read function, and async actions can be implemented by making the write function async and calling set when the result is ready.
The project notes that its fluid interface is intentional: atoms are described as monads, a pattern for modular, pure, and maintainable code. Documentation, a website, and a course are linked from the README. The library avoids string keys, which the README contrasts with Recoil.
Comments
0 people shared their preference · Deer Point appears after 10 participants
Sign in to join the discussion.