Skip to main content

Overview

An atom is the fundamental building block of Reatom’s state management system. It represents a mutable state container that can be read, updated, and subscribed to for changes. Atoms are reactive primitives that:
  • Store a single value of any type
  • Can be updated directly with new values
  • Automatically notify subscribers when their value changes
  • Track dependencies when read inside computed values

Creating Atoms

Basic Usage

Create an atom with an initial value:
Always provide a name as the second parameter for better debugging and DevTools support.

Type Signature

Reading State

Call an atom as a function to read its current value:
Reading an atom inside a computed value or effect automatically creates a dependency relationship.

Updating State

Use the .set() method to update an atom’s state:
Atoms are reactive primitives. You cannot pass arguments when calling them to read state - use .set() instead for updates.

Subscribing to Changes

Subscribe to an atom to be notified whenever its value changes:
The subscription callback is called immediately with the current value, then again whenever the value changes.

Lazy Initialization

Use a factory function for expensive initial state computation:

Advanced Patterns

Connected vs Disconnected

Atoms track whether they have active subscriptions:

Atom Metadata

Every atom has internal metadata accessible via __reatom:
The __reatom property is for internal use and advanced debugging. Avoid accessing it in application code.

Best Practices

Names help with debugging, logging, and DevTools integration:
Factory functions ensure initialization happens lazily:
Create multiple atoms instead of one large atom:

Common Use Cases

Form State

Toggle State

Counter

Computed

Derive state automatically from atoms

Actions

Encapsulate logic and side effects

Effects

Run reactive side effects

Extend

Add capabilities to atoms