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: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:
Subscribing to Changes
Subscribe to an atom to be notified whenever its 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:
Best Practices
Always name your atoms
Always name your atoms
Names help with debugging, logging, and DevTools integration:
Use factory functions for complex initial state
Use factory functions for complex initial state
Factory functions ensure initialization happens lazily:
Keep atoms focused and single-purpose
Keep atoms focused and single-purpose
Create multiple atoms instead of one large atom:
Common Use Cases
Form State
Toggle State
Counter
Related Concepts
Computed
Derive state automatically from atoms
Actions
Encapsulate logic and side effects
Effects
Run reactive side effects
Extend
Add capabilities to atoms