Similar to combineReducers()
in redux
, the combineForms()
helper function takes a forms
object where:
- each key is a string model path
- each value is either:
- (Any) an initial state, or
- (Function) an existing reducer
and turns it into a single reducing function (using combineReducers()
internally) where essentially:
- each key/value pair is a
modelReducer()
- a
'forms'
key on the same object is a singleformReducer()
that handles all forms for all models. (configurable inoptions
)
forms
(Object): An object whose keys correspond to relative string model paths and whose values correspond to:
- (Any) an initial state for the model, or
- (Function) an existing reducer for the model.
[model = '']
(String): The string representation of the parent model containing the child models in theforms
object. Defaults to an empty string.[options]
(Object): An options object to override the default options forcombineForms()
:
key
(String): The singleformReducer
key. Defaults to'forms'
.plugins
(Array): An array of plugins to pass into theformReducer()
. Defaults to no plugins.
(Function): A reducer which updates both the model state and the form state for each of the models inside forms
.
- The
model
provided tocombineForms()
must be the exact path in the state to the parent model. For most use-case scenarios, this is sufficient:
const initialUserState = {};
const initialGoatState = {};
const store = createStore(combineForms({
user: initialUserState,
goat: initialGoatState,
}));
However, for deeper parent models, the full path string must be provided:
const initialUserState = {};
const initialGoatState = {};
const store = createStore(combineReducers({
deep: combineForms({
user: initialUserState,
goat: initialGoatState,
}, 'deep'),
}));
// user model state accessed via 'deep.user'
// form state accessed via 'deep.forms'
// user form state accessed via 'deep.forms.user'