A set of operators to transform actions payload or an atoms state in a FRP style. Simply put, this package is convenient for reactive processing of actions and effects. But some operators could be useful for data processing too, like filter, delay (debounce, throttle) and sample.
Simple map utility, which allow you to receive previous dependency state by a second optional argument.
filter
Sometimes you already have filteredListAtom from the previous example and it have no internal memoization. So you could use filter operator to prevent extra updates.
Updates filtered by comparator function, which should return true, if new state should continue to propagate. It uses isShallowEqual from utils package by default.
This operator could filter actions too!
mapPayload
Map payload of each action call. Resulted action is not callable.
You could pass initial state by first argument to create an atom.
mapPayloadAwaited
Map fulfilled value of async action call. Resulted action is not callable.
You could pass initial state by first argument to create an atom.
mapInput
Create action which map input to passed action / atom.
debounce
Delay updates by timeout.
sample
Delay updates until other atom update / action call.
You can use parseAtoms to reduce the amount of . Let’s suppose you have the following structure:
And use it like this:
With parseAtoms you can refactor usage to look like this:
match
Creates an atom that depending on some condition or data patterns, which can be an atom too. Useful for describing UIs with @reatom/jsx or any other renderers. Here is the example of routing description from the base template↗ (Vite, TypeScript, React, Reatom).
You can call match with any primitive value, computed function, or existing atom. The returned atom depends on the initial expression and contains the undefined state by default. To add handlers and complete the state type, use chain methods. Each chain mutates the original atoms. It is a good practice to use it in the same place where atom was created.
default for replacing undefined fallback state
is for strict comparison
truthy (MDN↗) and falsy (MDN↗) for empty things handling
with structural handling
bind
Bind action or atom update function with passed callback.
withReset
Adds reset action to reset the atom state.
For example, clear state after all dependencies and subscribers are gone.
select
Sometimes you need to get computed value from an atom in another computer, but you don’t want to trigger other recomputations if the computed value is not changed. It is the common case for reatomComponent from reatom/npm-react package. The select allows you to perform and memorize some computation by a simple inline callback. It is specially useful when you can’t create separate memorized function because your target atom is dynamically created.
Under the hood select creates additional atom, so you can perform all regular tasks in the callback of the select, just like in the regular computed atom.
Important note is that you could use only one select in each atom, this is done for performance reasons and API simplicity. If, for some reason, you really need a few select, you could nest it and call one in another and so on.