React-redux-form: Possibility to skip store key with combineReducers

Created on 29 Sep 2016  路  7Comments  路  Source: davidkpiano/react-redux-form

Since probably 80 or more % of the apps will be using store not only with forms, combineReducers function will be used a lot. So, we will have:

forms: combineForms({
  user: initialUserState
}, 'forms')

Is it somehow possible to avoid using forms.user / forms.user.name and just user / user.name, maybe a convention?

enhancement

Most helpful comment

Sure, I can provide a mapForms function (I'm sure there's a better name than "mapForms", any suggestions?) that would simply give the object that combineForms uses in Redux's combineReducers():

// Let's come up with a better name than 'mapForms'
import { utils: { mapForms } } from 'react-redux-form';
import { createStore, combineReducers } from 'redux';

const store = createStore(combineReducers({
  existing: existingReducer,
  nonForm: nonFormReducer,
  ...mapForms({
    user: initialUserState,
    foo: initialFooState,
  }),
});

How does this look? You'd just use the spread operator to assign the user, foo, and generated forms reducers directly on the object passed into combineReducers, so that you can access model values by user.name and foo.name.

All 7 comments

Sure, I can provide a mapForms function (I'm sure there's a better name than "mapForms", any suggestions?) that would simply give the object that combineForms uses in Redux's combineReducers():

// Let's come up with a better name than 'mapForms'
import { utils: { mapForms } } from 'react-redux-form';
import { createStore, combineReducers } from 'redux';

const store = createStore(combineReducers({
  existing: existingReducer,
  nonForm: nonFormReducer,
  ...mapForms({
    user: initialUserState,
    foo: initialFooState,
  }),
});

How does this look? You'd just use the spread operator to assign the user, foo, and generated forms reducers directly on the object passed into combineReducers, so that you can access model values by user.name and foo.name.

Yes, this would be great. Will the Partial Models with just .name work as well?

Will the Partial Models with just .name work as well?

Yes, that's independent of this and will continue to work.

Should I call this createForms({ ... }) instead of mapForms({ ... }) ?

@davidkpiano createForms, mapForms, composeForms - any would be great, pick one you like the most, we'll be happy with your choice 馃槈

Waiting for this 馃帀 createForms sounds good.

Docs here: https://davidkpiano.github.io/react-redux-form/docs/api/createForms.html

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(''),
// }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

stutanne picture stutanne  路  4Comments

danielericlee picture danielericlee  路  3Comments

kallemakynen picture kallemakynen  路  4Comments

mikkelwf picture mikkelwf  路  3Comments

Jokinen picture Jokinen  路  3Comments