recoil
Recoil is an experimental state management library for React developed by Facebook that provides a graph-based approach to shared state. Recoil introduces atoms…
Installation
npm install recoil
yarn add recoil
pnpm add recoil
Import
import { atom, useRecoilState } from 'recoil';Quick Example
import { atom, selector, useRecoilState, useRecoilValue } from 'recoil';
const countAtom = atom({ key: 'count', default: 0 });
const doubleSelector = selector({
key: 'double',
get: ({ get }) => get(countAtom) * 2,
});
function Counter() {
const [count, setCount] = useRecoilState(countAtom);
const double = useRecoilValue(doubleSelector);
return <button onClick={() => setCount(c => c + 1)}>{count} ({double})</button>;
}About recoil
Recoil is an experimental state management library for React developed by Facebook that provides a graph-based approach to shared state. Recoil introduces atoms — units of state that components can subscribe to — and selectors — pure functions that derive state from atoms or other selectors. When an atom's value changes, only components that subscribe to that specific atom re-render, providing fine-grained reactivity without context-based re-render cascading. Atoms are created with atom({ key: 'unique-key', default: initialValue }) and accessed in components with useRecoilState(myAtom) or useRecoilValue(myAtom). Selectors compute derived state asynchronously or synchronously: selector({ key: 'filtered', get: ({get}) => get(listAtom).filter(predicate) }). Recoil supports atom families for parameterized state (creating atom instances from a parameter), selector families for parameterized derived state, and snapshots for observing and time-traveling through state history. The library handles concurrent mode compatibility, which was a key motivation for its development at Facebook. While Recoil's development has slowed and its experimental status has led many teams to prefer Jotai (which offers a similar atomic model), Recoil's concepts have significantly influenced the React state management landscape.
Quick Facts
| Package | recoil |
| Category | State Management |
| Weekly Downloads | 400K+ |
| License | MIT |
| Install | npm install recoil |
Related Packages
Jotai is a primitive and flexible state management library for React that takes an atomic approach i…
Zustand is a small, fast, and scalable state management library for React that provides a simple hoo…
Redux is a predictable state container for JavaScript applications, most commonly used with React bu…
React is the most popular JavaScript library for building user interfaces, developed and maintained …
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.