📦
State Management400K+/wkMIT

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
npm install recoil
yarn
yarn add recoil
pnpm
pnpm add recoil

Import

ESM
import { atom, useRecoilState } from 'recoil';

Quick Example

usage
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

Packagerecoil
CategoryState Management
Weekly Downloads400K+
LicenseMIT
Installnpm install recoil

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.