# Derived & Async Atoms

> Compose atomic state dynamically and trigger native React Suspense with promises.

Subject: State Management | Lesson 20 of 22 | 7 min read
Interactive lesson: https://codewithanimation.in/state-management/jotai-derived-and-async-atoms
Previous: [Atomic State (Jotai)](https://codewithanimation.in/state-management/atomic-state-jotai.md) | Next: [Preact Signals & Bypassing the VDOM](https://codewithanimation.in/state-management/signals-bypassing-vdom.md)

## Composition & Promises

A Derived Atom is created by passing a read function `(get) => ...` to `atom()`. By calling `get(anotherAtom)` inside this function, Jotai establishes a dynamic, runtime dependency graph.

An Async Atom is simply a derived atom whose read function returns a Promise (`async (get) => ...`).

## No More useEffect

In standard React, coordinating dependent state variables or firing network requests when an ID changes often results in spaghetti `useEffect` chains.

Jotai handles this reactivity graph for you. When a base atom updates, any derived atoms automatically re-evaluate. If that evaluation is a fetch request, it happens seamlessly.

## Native React Suspense

When a React component calls `useAtomValue()` on a pending Async Atom, Jotai automatically throws the Promise to React.

This triggers the nearest `<Suspense>` boundary natively, allowing you to build loading states without manually tracking `isLoading` or `isError` flags in your global store.

## Common mistake: The isLoading Boilerplate

In monolithic stores, fetching data means you must define `isLoading` and `error` state properties, write separate actions to toggle them, and manually handle race conditions.

With Jotai Async Atoms, the pending Promise *is* the loading state. React Suspense handles the rest.

## Tip: Dynamic Tracking

Jotai tracks dependencies dynamically *at runtime*. If your derived atom has an `if` statement, Jotai will only subscribe to the atoms that were actually executed during that specific render pass!

## Try it: Interactive Lab: Async Suspense

Use the controls to change the User ID. The async atom will automatically refetch, trigger React Suspense, and animate the UI!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/jotai-derived-and-async-atoms

## Check yourself

### Question 1

What happens by default when a React component reads an Async Atom that is still resolving its Promise?

- A. The component returns `undefined` until the promise resolves.
- B. Jotai automatically throws the pending Promise, triggering the nearest React `<Suspense>` boundary.
- C. The app crashes unless you wrap the atom in a try/catch block.

Correct answer: B

Jotai integrates natively with React Suspense. When an async atom is pending, it suspends the component rendering.

- Why not A: Jotai doesn't silently return undefined; it hooks into React's built-in async handling.
- Why not C: Errors trigger ErrorBoundaries, but pending promises trigger Suspense.

## Remember this

- Derived atoms are created by passing a function `(get) => ...` to `atom()`.
- Jotai tracks dependencies dynamically at runtime on every evaluation.
- Async Atoms are just derived atoms that return a Promise.
- Jotai natively suspends components while Async Atoms are pending.
