-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Question about function combineReducers with multiple reducer module imports #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imports are object. Just like you'd merge two objects: import * as reducers1 from './reducer1';
import * as reducers2 from './reducer2';
const allReducers = Object.assign({}, reducers1, reducers2);
const reducer = combineReducers(allReducers); |
Thanks for such fast reply; didn't notice that ES6 has added such function. 👍 |
@TheDeveloperXYZ We're actually going to remove
You can use something like Lodash's |
@gaearon Good idea to remove |
Is it possible to multiple call |
It just desugars to You can even write a reasonable approximation by hand: let finalReducers = {}
Object.keys(reducers).forEach(key => finalReducers[key] = reducers[key])
finalReducers.routing = routerReducer
const reducer = combineReducer(finalReducers) It’s all just JavaScript! |
Thanks for fast response! I guess I was doing something in a wrong way, but I can't understand what exactly. But thank you very much about your approach with using |
If you see If you can publish a project reproducing this I'm happy to take a look. |
I do it like this: mainReducer.js import {combineReducers} from 'redux'
import {routerReducer} from 'react-router-redux'
// list of reducers
import registerReducer from 'registerReducer'
import homeReducer from 'homeReducer'
import aboutReducer from 'aboutReducer'
import contactUsReducer from 'contactUsReducer'
export const mapStateToProps = (state) => {
return {
register: state.register,
home: state.home,
about: state.about,
contact:state.contact
}
}
export default combineReducers({
routing: routerReducer,
register: registerReducer,
home: homeReducer,
about: aboutReducer,
contact: contactUsReducer
}) There is function mapStateToProps in there. The reason is because I often forgot to add entry to mapStateToProps function when it is put in other file. So I bring it to my main reducer,and for easy access. Open multiple files to make action and reducer is real pain. |
Hello.
Let's imagine that we have a reducer file like this:
There are also 3 other similar reducer files with multiple exports.
While the document merely gives an example how to use combineReducers with only 1 file with multiple exports - combineReducers | Redux (import * as reducers from './reducers';)
Then how do you use that function with multiple imports like this?
Thanks!
The text was updated successfully, but these errors were encountered: