createForms(forms, [model], [options])

import { combineReducers } from 'redux';
import { createForms } from 'react-redux-form';

const initialUserState = {};
const initialGoatState = {};

const reducer = combineReducers({
  foo: fooReducer,
  bar: barReducer,
  ...createForms({
    user: initialUserState,
    goat: initialGoatState,
  }),
});

// reducer state shape will be:
// {
//   foo: fooReducer,
//   bar: barReducer,
//   user: modelReducer('user', initialUserState),
//   goat: modelReducer('goat', initialGoatState),
//   forms: formReducer(''),
// }

The createForms() 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 an object where:

  • each key/value pair is a modelReducer()
  • a 'forms' key on the same object is a single formReducer() that handles all forms for all models. (configurable in options)

Arguments

  1. 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.
  2. [model = ''] (String): The string representation of the parent model containing the child models in the forms object. Defaults to an empty string.
  3. [options] (Object): An options object to override the default options for combineForms():

    • key (String): The single formReducer key. Defaults to 'forms'.
    • plugins (Array): An array of plugins to pass into the formReducer(). Defaults to no plugins.

Returns

(Object): An object that is the mapping of each reducer or initial state to a modelReducer() and a .forms prop that contains the formReducer.

Notes

  • The createForms() function is meant to be used with combineReducers() from redux. Use it when you want a flatter combined reducer structure.

results matching ""

    No results matching ""