RjObject
RjObject is the base type of RocketJump it's the container that represent a piece of your "data source logic".
Concretely an RjObject contains:
- An internal state described by his reducer.
- A proxy between the internal state and the computed state used by consumers.
- A side effect(s) in form of Observables.
- A set of action creators functions that triggers side effects and/or state updates.
- An optional friendly name used for debug purpose.
Create#
You can create RjObjects using the rj() function.
The only mandatory input to create an RjObject
is an effect function of type: (...args) => Promise | Observable.
Consuming#
When consuming an RjObject from React hooks or HOCs you receive from them some state and action creators.
Default state#
The rj() defaults are designed to store the last effect results
along with some meta information and give to consumers a state with this shape:
data: The last effect result,nullon start.pending: A flag that indicates if your effect is in flying.error: The eventually effect error,nullon start.
Default action creators#
The rj() defaults action creators are designed to work with the default state and
given effect and are:
run(...args: any[]): trigger effect,...argsare passed as effect inputs, setpendingtotrueanderrortonull,cancel(): cancel effect execution, setpendingtofalse.clean(): cancel effect execution, setpendingtofalse,dataanderrortonull.updateData(newData): set thedatavalue tonewData.
React example#
This is a complete example using the concept exposed above:
note
Returning clean in useEffect is only needed if you want to clear data value
when id changes.
The reason isn't to avoid setting state on unmounted component.
ALL effect are guarantee to be canceld when component unmount.
You can write it in a more concise way using the useRunRj hook instead of useRj,
learn more on how to cunsume RjObjects.