Action Creators
Every RjObject has its own action creators: a collection functions that trigger state updates and/or side effects.
#
Plain and effect action creatorsIn RocketJump there is two type of action creators:
#
Plain action creatorsPlain action creators returns plain actions and theese actions are dispatched on the reducer.
#
Effect action creatorsEffect action creators returns special actions. This actions becomes a stream, side effects take this stream as inputs and emit plain actions dispatched on the reducer.
You can find action creators under actionCreators
property in your RjObject.
When you consume an RjObject using RocketJumps hooks or HOCs theese actions creators are atomatically bind to reducer dispatch and side effects.
#
Default action creatorsWhen you crafting a new RjObject default action creators are generated. Default action creators are designed to work with default state shape and side effects, and are:
updateData(data: any): { type: 'UPDATE_DATA' }
: A plain action creator that update your data state.run(...args: any[]): EffectAction<'RUN'>
: An effect action creator that run your effect.cancel(): EffectAction<'CANCEL'>
: An effect action creator that cancel your effect.clean(): EffectAction<'CLEAN'>
: An effect action creator that cancel your effect and reset your root state.
#
Adding plain actionsTo adding plain actions you can use the actions
property in rj constructor
or the actions
method when using Builder syntax,
learn more about builder invocation vs function invocation.
The actions
method should be a function with this signature:
nextActions
are merged to currentActions
.
So combining the concept learned in the previous chapters let's build a counter using RocketJump!
#
Adding effect actionsTo create an effect action you can use an helper from RocketJump called
makeEffectAction
:
The signature of makeEffectAction is:
When your EffectAction is emitted to your side effect the shape you can expect is:
The default run()
effect action creator will create an effect action EffectAction<'RUN'>
.
Side effects dispatch the RUN
action immediately:
note
The meta passed to your makeEffectAction
are injected in all emitted action.
Then depending of your side effect configuration when the effect is executed,
using params
as input, the PENDING action is dispatched:
If the effect resolves / complete a SUCCESS action will emitted.
The payload.data
contains the result of effect.
If the effect rejects / errors a ERROR action will emitted.
The payload
contains the error generated from effect.
When creating new effect action creators using the rj constructor you can
add meta to current actions using the special withMeta
method.
In this exaple we use the meta append
to append to our data instead
of replace them.
For this example our immaginary REST API /api/products
will return a JSON
with the following shape: