davidkpiano / react-redux-form

Create forms easily in React with Redux.
https://davidkpiano.github.io/react-redux-form
MIT License
2.06k stars 252 forks source link

[PROPOSAL] Higher-order reducer/reducer enhancer - "modellable()" #11

Closed davidkpiano closed 8 years ago

davidkpiano commented 8 years ago

What does everybody think about providing a higher-order reducer (i.e. reducer enhancer) for creating a model reducer on top of an existing reducer? Code would look something like this:

import { modellable } from 'react-redux-form';

// existing user reducer
import userReducer from './user-reducer';

// modellable: (reducer, model) => reducer'
export default modellable(userReducer, 'user');

This would create a reducer that:

  1. Updates the state if the action is a RRF action (such as actions.change or actions.reset)
  2. Pass the updated state to the original reducer, as well as the action

This can also simplify the updating of stores in existing projects to retrofit the model reducers:

+ import { modellable } from 'react-redux-form';

const store = createStore(combineReducers({
-  user: userReducer,
-  foo: fooReducer
+  user: modellable(userReducer, 'user'),
+  foo: modellable(fooReducer, 'foo')
}));

This would not replace the existing createModelReducer(), which is quite useful when making new reducers.

In case "modellable" is too awkward to spell (or, as it might turn out, probably not even a word), we can call the enhancer:

Just some ideas. Thoughts?

davidkpiano commented 8 years ago

:airplane: LANDED in react-redux-router@0.4.1: https://github.com/davidkpiano/react-redux-form/commit/95ad41c8199c36b70c08145d5a4cff74e60d4b8f :tada: