State
Local state is represented as Atom-like data type wrapping React's state hook.
state
takes initial value and schedules component's update every time it is updated with either reset!
or swap!
.
When initial value has to be computed before it’s used, it is recommended to pass it in a callback to state
. The callback will be executed only once, in component’s initialization stage.
Initializing and interacting with local state is only possible from within UIx components. But that doesn't mean that state cannot be declared in a separate function that gets called from a component.
Reusable state
Because state is not directly coupled with components it is possible to build reusable behaviors on top of it. In the example below we create use-validation
function that encapsulates logic that keeps track of a value and applies updates when the value satisfies provided validation function.
Last updated