Skip to main content

AtomConfig

An AtomConfig object is the 3rd argument passed to the atom() factory.

Definition

interface AtomConfig {
dehydrate?: <D>(state: State) => D
flags?: string[]
hydrate?: <D>(dehydratedState: D) => State
manualHydration?: boolean
ttl?: number
}

All fields are optional.

dehydrate

A function that accepts the current state of an instance of this atom and transforms it into a serializable value.

Note that dehydrate doesn't have to return a string. Any serializable value will do, including JS objects and arrays.

This function is called by Zedux whenever ecosystem.dehydrate() is called without { transform: false }. It's called once for every instance of this atom that is to be included in the dehydration.

The primary use of dehydrate is to transform a non-serializable JS value into a serializable representation of it. For example when working with JS maps, sets, or similar data types from 3rd-party libraries, you can use dehydrate to improve logging or enable SSR.

You will usually pair this with hydrate.

flags

An array of strings. These flags correlate to the ecosystem's flags property. See that page for an example.

hydrate

A function that accepts a serializable version of an atom instance's value and transforms it into the proper data type expected by instances of this atom.

The received value will usually be whatever was previously returned from a dehydrate call.

This function is called by Zedux whenever an atom instance of this atom is hydrated after a corresponding hydration has been set in the ecosystem via ecosystem.hydrate() . It's called once for every instance of this atom that is to be hydrated.

You will probably always pair this with dehydrate.

manualHydration

A boolean. If set to true, this prevents Zedux from automatically hydrating instances of this atom.

The default hydration flow can often make atom instances evaluate twice unnecessarily. Use this combined with injectStore()'s { hydrate: true } option to hydrate the atom instance properly on initial evaluation, preventing the double-evaluation.

ttl
A number. How long instances of the atom should stick around when stale. See AtomTemplate#ttl.

See Also