There have been lots of data-management frameworks for ReactJS. Notably Dan Abramov's React Redux, and the corresponding Thunk.
However, there are several caveats of React Redux:
- Requiring
Providerwhen usingReact Redux. - A single global state, which can contaminate with each other for different reducers.
- Actions and reducers are kind of redundant with each other, as we can:
- simply provide action objects to the reducers, and let the reducers do all the computation for reduction, or
- have the actions do all the computations and provide the data objects to the reducers, and let the reducers simply put the data objects to the corresponding states.
- Requiring
dispatchevery time we want to do some actions.
use-thunk intends to simplify react redux with the following goals:
- Same as
react redux, separating components and reducers. - There is no need to differentiate between actions and reducers.
- reducers: use
setDatato doObject.assign({}, state, action-object). - actions: do all the computation and do
dispatch(setData(myID, action-object))multiple times. - For easy of understanding, actions/reducers are integrated and named as "thunk" in this repository and starting from this sentence.
- reducers: use
- When doing programming:
- The thunk should be like programming a typical
js/tsmodule. - The components should be focusing only on component-rendering, with as least data manipulation as possible.
- There is no need to specify
dispatchin components. - Following modularization pattern, each class of the thunk has its own state. Different thunks cannot directly access the states of other thunks. We can access the states only indirectly through the functions of the thunks.
- The thunk should be like programming a typical
To achieve the goals:
- With the concept of "normalized state" in mind:
State: the state of each object.NodeState: the metadata of each object, including theidandState.ClassState: the collection ofNodeStateby reducer-module.
- Heavily use the concept of
Thunk, to be able to have multiple reductions in one operation. - The thunk functions are automatically attached with
dispatchin the react components. - The thunk functions are still the unattached functions in the thunk modules. We still need to use
dispatchwhen calling these functions in the thunk modules.
Primitive reducers are similar to the original "redux' reducers".
There is actually also the "redux' action" part in a primitive reducer.
However, the purpose of the "redux' action" part is only to provide
the object-based actions ({}) to the "redux' reducers".
The map between the primitive reducer actions (redux' actions)
and primitive reducer functions (redux' reducers)
is defined in DEFAULT_REDUCE_MAP.
Primitive reducers are simply for internal reductions for the thunk modules
and are not supposed to be accessible in the components.
We provide the following default primitive reducers:
- init: initialize an object-state.
- setData: update the object-state.
- remove: remove the object-state.