On Construct
A function that runs before the store is instantiated. Returns the first state of the store.
When creating a store factory with newStoreDef
, you can define a constructor function to run before the store is instantiated via onConstruct
.
How does it look like:
You can define a simple and high level initial props for the user to pass, and transform it to the state of the store.
Fetching Data on Initialization
The onConstruct
function can be async.
Example
- Define how the store is initialized
- Display the loading state
I'm working on suspense support based on read, so the selector suspends your component if the value is not ready yet.
What if initial props changes?
If the initial props changed, you have two options:
- Keep the same store instance and invoke some store action to update the state.
- Create a new store instance with the new initial props.
Reach to the 1. when these props are just trivial properties of the store.
Reach to the 2. when these props are what represent the store, like if the entity id changed, the whole thing should be nuked and recreated.
Example
When trivial properties change:
When the store is the entity:
Cleaning up
The onConstruct
function receives a abort signal that let you clean up any side effect caused by the function.
Example
When the clean up happens
The abort signal used on the onConstruct
function is aborted when the store is disposed, either by calling it manually via store.dispose()
or unmounting the component where the useNewStore
hook was used.
The primitive (in depth)
onConstruct
runs before the store’s first state is created. It maps the user’s initial props into a plain source state that the reducer will use to build the final shape.
Return only source fields from onConstruct
. Do not include $
derived fields here. Declare your derived fields in the reducer. The reducer will be called in the construction pass. If you pass derived values here, they will be ignored and overwritten by the reducer.
Saphyra calls the reducer once with prevState
as {}
and the onConstruct
result as state
. Since the fields are being first evaluated, all diffs are gonna be triggered, including derived fields and effects.
If any async effect is triggered during this reducer pass, Saphyra runs it under the ["bootstrap"]
transition to scope loading and side effects to the initialization phase.
Both the onConstruct
promise and all async effects started during construction complete under the reserved ["bootstrap"]
transition, keeping initialization behavior predictable and traceable.
Minimal example
Derived values
This example shows how to derive values from the source in the onConstruct
function.
Initial Async Effect
This example shows async effects can be leveraged in the store initialization.