Need help with react-atom?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

144 Stars 6 Forks MIT License 135 Commits 23 Opened issues


A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs

Services available


Need anything else?

Contributors list

react-atom logo

A simple way to manage shared state in React

Built on the React Hooks API

Inspired by atoms in reagent.cljs

TypeScript npm (scoped) npm bundle size (minified) npm bundle size (minified + gzip)

Build Status codecov npm

NpmLicense Commitizen friendly semantic-release


provides a very simple way to manage state in React, for both global app state and for local component state: ✨

Put your state in an

import { Atom } from "@dbeining/react-atom";

const appState = Atom.of({ color: "blue", userId: 1 });

Read state with

You can't inspect

state directly, you have to
erence it, like this:
import { deref } from "@dbeining/react-atom";

const { color } = deref(appState);

Update state with

You can't modify an

directly. The main way to update state is with
. Here's its call signature:
function swap(atom: Atom, updateFn: (state: S) => S): void;

is applied to
's state and the return value is set as
's new state. There are just two simple rules for
  1. it must return a value of the same type/interface as the previous state
  2. it must not mutate the previous state

To illustrate, here is how we might update

's color:
import { swap } from "@dbeining/react-atom";

const setColor = color => swap(appState, state => ({ ...state, color: color }));

Take notice that our

is spreading the old state onto a new object before overriding
. This is an easy way to obey the rules of

Side-Effects? Just use

You don't need to do anything special for managing side-effects. Just write your IO-related logic as per usual, and call

when you've got what you need. For example:
const saveColor = async color => {
  const { userId } = deref(appState);
  const theme = await post(`/api/user/${userId}/theme`, { color });
  swap(appState, state => ({ ...state, color: theme.color }));

Re-render components on state change with the ✨
✨ custom React hook

is a custom React Hook. It does two things:
  1. returns the current state of an atom (like
    ), and
  2. subscribes your component to the atom so that it re-renders every time its state changes

It looks like this:

export function ColorReporter(props) {
  const { color, userId } = useAtom(appState);

return (

User {userId} has selected {color}

{/* useAtom hook will trigger a re-render on swap */} swap(appState, setRandomColor)}>Change Color
); }

Nota Bene: You can also use a selector to subscribe to computed state by using the
argument. Read the docs for details.

Why use

😌 Tiny API / learning curve
`Atom.of`, `useAtom`, and `swap` will cover the vast majority of use cases.
🚫 No boilerplate, just predictable state management
Reducers? Actions? Thunks? Sagas? Nope, just `swap(atom, state => newState)`.
🎵 Tuned for performant component rendering
The useAtom hook accepts an optional select function that lets components subscribe to computed state. That means the component will only re-render when the value returned from select changes.
😬 React.useState doesn't play nice with React.memo
useState is cool until you realize that in most cases it forces you to pass new function instances through props on every render because you usually need to wrap the setState function in another function. That makes it hard to take advantage of React.memo. For example:
function Awkwardddd(props) {
  const [name, setName] = useState("");
  const [bigState, setBigState] = useState({ ...useYourImagination });

  const updateName = evt => setName(;
  const handleDidComplete = val => setBigState({ ...bigState, inner: val });

  return (
      <input type="text" value="{name}" onchange="{updateName}">
      <expensivebutmemoized data="{bigState}" oncomplete="{handleDidComplete}"></expensivebutmemoized>

Every time input fires onChange, ExpensiveButMemoized has to re-render because handleDidComplete is not strictly equal (===) to the last instance passed down.

The React docs admit this is awkward and suggest using Context to work around it, because the alternative is super convoluted.

With react-atom, this problem doesn't even exist. You can define your update functions outside the component so they are referentially stable across renders.

const state = Atom.of({ name, bigState: { ...useYourImagination } });

const updateName = ({ target }) =&gt; swap(state, prev =&gt; ({ ...prev, name: target.value }));

const handleDidComplete = val =&gt;
  swap(state, prev =&gt; ({
    bigState: { ...prev.bigState, inner: val }

function SoSmoooooth(props) {
  const { name, bigState } = useAtom(state);

  return (
      <input type="text" value="{name}" onchange="{updateName}">
      <expensivebutmemoized data="{bigState}" oncomplete="{handleDidComplete}"></expensivebutmemoized>
TS First-class TypeScript support
react-atom is written in TypeScript so that every release is published with correct, high quality typings.
👣 Tiny footprint
react-atom minified file size react-atom minified+gzipped file size
⚛️ Embraces React's future with Hooks
Hooks will make class components and their kind (higher-order components, render-prop components, and function-as-child components) obsolete. react-atom makes it easy to manage shared state with just function components and hooks.


npm i -S @dbeining/react-atom


has one bundled dependency, @libre/atom, which provides the Atom data type. It is re-exported in its entirety from
. You may want to reference the docs here.

also has two
, namely,
[email protected]^16.8.0
[email protected]^16.8.0
, which contain the Hooks API.




Code Example:
in action

Click for code sample
import React from "react";
import ReactDOM from "react-dom";
import { Atom, useAtom, swap } from "@dbeining/react-atom";

//------------------------ APP STATE ------------------------------//

const stateAtom = Atom.of({
  count: 0,
  text: "",
  data: {
    // ...just imagine

//------------------------ EFFECTS ------------------------------//

const increment = () =&gt;
  swap(stateAtom, state =&gt; ({
    count: state.count + 1

const decrement = () =&gt;
  swap(stateAtom, state =&gt; ({
    count: state.count - 1

const updateText = evt =&gt;
  swap(stateAtom, state =&gt; ({

const loadSomething = () =&gt;
    .then(res =&gt; res.json())
    .then(data =&gt; swap(stateAtom, state =&gt; ({ ...state, data })))

//------------------------ COMPONENT ------------------------------//

export const App = () =&gt; {
  const { count, data, text } = useAtom(stateAtom);

  return (
      <p>Count: {count}</p>
      <p>Text: {text}</p>

      <button onclick="{increment}">Moar</button>
      <button onclick="{decrement}">Less</button>
      <button onclick="{loadSomething}">Load Data</button>
      <input type="text" onchange="{updateText}" value="{text}">

      <p>{JSON.stringify(data, null, "  ")}</p>

ReactDOM.render(<app></app>, document.getElementById("root"));

🕹️ Play with
in CodeSandbox 🎮️

You can play with

live right away with no setup at the following links:

| JavaScript Sandbox | TypeScript Sandbox | | ------------------------------- | ------------------------------- | | try react-atom | try react-atom |

Contributing / Feedback

Please open an issue if you have any questions, suggestions for improvements/features, or want to submit a PR for a bug-fix (please include tests if applicable).

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.