Reducer
Every RjObject has its own reducer that describe its internal state and how this state changes in response of actions.
#
Default reducerIf you don't specify anything the default reducer crafted when creating a RjObject has this state shape:
info
When consuming an RjObject the default computed state is the state under root key.
RocketJump use redux style actions:
The action types handled by this reducer are:
- PENDING
PENDING action is dispatched when your effect starts default root reducer implementation make pending true
.
- SUCCESS
SUCCESS action is dispatched when your effect resolves / complete
the data
key on payload
contains the effect result.
Default root reducer implementation make pending false
and fill the data
key with payload.data
from action.
- FAILURE
FAILURE action is dispatched when your effect rejects / error
the payload
key on action contains the error generated from effect.
Default root reducer implementation make pending false
and fill the error
key with payload from action.
- UPDATE_DATA
UPDATE_DATA action is dispatched from default action creator updateData
.
Default root reducer implementation set the payload
as new data.
- CANCEL
CANCEL action is dispatched from default action creator cancel
.
Default root reducer implementation set the pending
to false
.
- CLEAN
CLEAN action is dispatched from default action creator clean
.
Default root reducer implementation set reset pending
, data
and error
to
default state:
In addition when your RjObject is initialized the INIT action is also dispatched.
Finally the last RocketJump core action is the RUN
action.
Dispathed when the effect is triggered.
Default root reducer implementation doesn't listen to this action.
INIT, RUN, CANCEL, CLEAN, PENDING, SUCCESS, FAILURE
and UPDATE_DATA action types are all export from 'react-rocketjump'
module:
We call the reducer under the root
key the root reducer, you can extend the root reducer or add others reducers under specific keys.
You can find reducer under reducer
property in your RjObject.
#
Extending root reducerYour can extend the root reducer using the reducer
property in rj constructor or the reducer
mehtod when using Builder syntax, learn more about builder invocation vs function invocation.
This option should be a function with the old reducer as input and the new reducer as output.
Tipically the reducer
option is used when you want overwrite the default reducer or your want to change default behaviour.
If your only needs is to add a new action handled by default reducer you can use the composeReducer
option.
This option should be a reducer with the same shape of default reducer and it's used to compose your reducer.
The final reducer has the default reducer plus can handle the APPEND action type.
#
Adding new reducersYou can adding new reducers next to root reducer using combineReducers
option, this option must be an object indexed by the reducers you want to add, under the hood the old but good redux combineReducers is used.
Reducers provided in combinerReducers still has access to ALL actions.