selector
Signature
selector<T>(getter: (get: GetFn) => T): Selector<T>valdres Derive computed state that updates automatically
Creates derived state that automatically recomputes when its dependencies change. Selectors are read-only — they compute their value from atoms and other selectors.
Usage
import { atom, selector } from "valdres"
const firstNameAtom = atom("John")
const lastNameAtom = atom("Doe")
const fullNameSelector = selector(get => {
const first = get(firstNameAtom)
const last = get(lastNameAtom)
return `${first} ${last}`
})
Parameters
| Parameter | Type | Description |
|---|---|---|
getter | (get: GetFn) => T | A function that computes the derived value. Use get() to read atoms or other selectors. |
options.name | string | Optional name for debugging, devtools, and validation error messages |
options.schema | Schema<T> | Schema for runtime validation of the selector's result, and type inference. A no-op unless validation is enabled. See Schema Validation. |
options.schemaValidation | boolean | Per-selector override of the store's schemaValidation flag — true always validates this selector, false exempts it |
Async selectors
Selectors can return a promise. They work with Suspense in React. Note that
the getter itself must be a sync function that returns a promise — selector()
rejects async functions at creation time:
const userDataSelector = selector(get => {
const userId = get(userIdAtom)
return fetch(`/api/users/${userId}`).then(res => res.json())
})
Schema validation
A selector's schema validates its computed result on every evaluation — a
runtime contract that catches a selector drifting from its declared shape:
const fullNameSelector = selector(
get => `${get(firstNameAtom)} ${get(lastNameAtom)}`,
{ name: "fullName", schema: z.string() },
)
Validation is opt-in per store (store({ schemaValidation: true })) or per
selector (schemaValidation: true). See the
Schema Validation guide.
See also
- selectorFamily — create a collection of selectors keyed by a parameter
- Schema Validation — runtime validation and type inference
- injectValue — read a selector value in your components