-
Notifications
You must be signed in to change notification settings - Fork 28
Description
When working on medium-sized apps, it's helpful to enforce the separation of concerns by writing separate reducers for different parts of the state tree. An example of this is routing which needs its own state and logic.
I'd like to add an optional combineReducers function to innerself. Perhaps in a new module: innerself/combine?
@bsouthga created his own implementation in innerself-hg and I used this one in A moment lost in time.
I wonder if we should also add selectors to connect. innerself actually used to have selectors when I first wrote it. I then removed them to keep things simple.
Selectors are functions which select a part of the state before it's passed on to the connected component. Something like:
const ConnectedFoo = connect(state => state.foo)(FooComponent);In Redux selectors are super-useful: they help Redux decide if the connected component needs to update. In innerself everything re-renders at all times, so the main benefit would be decoupling the shape of the state tree from the API of individual components.
Even though this looks nice and makes the code self-documenting, I expect the most common scenario to not need selectors. We'd then force the connect(state => state)(…) boilerplate onto everyone or have a special-case for the undefined selector: connect()(…). This in turn would be different from how Redux treats the undefined selector: it ignores the store completely. Perhaps that's OK. If you want to ignore state, you simply don't connect your component to it in innerself.
For now in my code I went for the following pattern whenever I needed selectors:
const ConnectedFoo = connect((state, ...args) => FooComponent(state.foo, ...args));This pattern could be easily abstracted to a helper function, for instance:
function select(selector, component) {
return (state, ...args) => component(selector(state), ...args);
}And then:
const ConnectedFoo = connect(select(state => state.foo, FooComponent)) This helper could also live in the optional module alongside combineReducers. I'm not particularly fond of this API, though.