-
Notifications
You must be signed in to change notification settings - Fork 0
How to create dynamic reducer in combinereducers? #2
Comments
hey @nguyenhuutinh, If I understood you correctly, you want to create multiple components that are using the same reducer and actions? So yes, this is exactly what this lib does. It allows u to create multiple instances of the same components ( If I did not answer your question please elaborate what is the issue with |
Hey @jony89 I am also expecting the same, let me put this question in other way. export default combineReducers({ |
Hey @sivakumar-cf , that is exactly what this library does. So the code you wrote is exactly what you need to do, the export const MyFirstContainerConnected = connect(
state => ({ ...state.firstContainerReducer }),
actionCreatorsInterceptor(actionCreators, 'INSTANCE1'),
)(MyContainer); where are you facing an issue? |
You can generate random strings for the N instances. you just need to make sure its the same value that is given to actionCreatorsInterceptor during the creation of a new container. |
My issue is that how do I dynamically build the reducer and passing to container depends on number of instance of the same container, for example. here my reducer is uniqueId as a variable which I want to add to the redux store.
and in combineReducers, I want to include reducerInterceptor dynamically as well. |
You can try and generate random uniqueIds, For example : file1: export const dynamicStrs = [
'random1',
'random2',
]; file2: (reducers) import { dynamicStrs } from '../..';
const dynamicReducers = {};
dynamicContainers.forEach(id => {
dynamicReducers[id] = reducerInterceptor(someGenericReducer, id);
});
export default combineReducers(dynamicReducers); and the same in the containers file3 for the connect function. export const MyFirstContainerConnected = connect(
state => ({ ...state[id] }),
actionCreatorsInterceptor(actionCreators, id),
)(MyContainer); |
How to create dynamic reducer?
export default combineReducers({
firstContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE1'),
secondContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE2'),
});
I call API to get the category list and theirs products.
So I want to use multi instance of Product Components. But the issue is have to create multi action . Do your solution can resolve my problem?
The text was updated successfully, but these errors were encountered: