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.
#
CreateYou can create RjObjects using the rj()
function.
The only mandatory input to create an RjObject
is an effect function of type: (...args) => Promise | Observable
.
#
ConsumingWhen consuming an RjObject from React hooks or HOCs you receive from them some state and action creators.
#
Default stateThe 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,null
on start.pending
: A flag that indicates if your effect is in flying.error
: The eventually effect error,null
on start.
#
Default action creatorsThe rj()
defaults action creators are designed to work with the default state and
given effect and are:
run(...args: any[])
: trigger effect,...args
are passed as effect inputs, setpending
totrue
anderror
tonull
,cancel()
: cancel effect execution, setpending
tofalse
.clean()
: cancel effect execution, setpending
tofalse
,data
anderror
tonull
.updateData(newData)
: set thedata
value tonewData
.
#
React exampleThis 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.