unistore
unistore copied to clipboard
combineReducers
Hello! Tell me, please, how i can join reducers, like "combineReducers" in Redux?
Hmm - do you have an example of where this is needed?
function combineReducers(...fns) {
return (...args) => Object.assign(...fns.map( fn => fn(...args) );
}
I think no need for a combine reducers.
/ root
| -- actions
| | -- someActions.js // returns an object that handles **some actions**
| | -- anotherActions.js // returns an object that handles **another actions**
| ` -- index.js // returns all the actions spread on an object
`-- store.js
someActions.js
export default {
one (state) {
// do what?
},
two (state) {
// do what?
}
};
anotherActions.js
export default {
one (state) {
// do what?
},
two (state) {
// do what?
}
};
index.js
import someActions from './someActions';
import anotherActions from './anotherActions';
export default {
...someActions,
...anotherActions
};
store.js
import createStore from 'unistore';
import actions from './actions';
const store = createStore({
// what?
});
const actions = store => ({ ...actions });
export { actions };
export default store;
now you can access them.
import { Provider, connect } from 'unistore/preact';
import store, { actions } from './store';
const App = connect('count', actions)(
({ count, increment }) => (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
)
)
export default () => (
<Provider store={store}>
<App />
</Provider>
)
@aprilmintacpineda in this case we connect all our app actions into out component.
Sometimes we'd like to have a subset of actions like
connect(props, {...auth, ...catalog})(props => (...))
because not every screen/component needs all set of actions of our app. If #136 was implemented, that'd be enough to
props => {
let {login, logout} = useActions(auth)
let {fetch} = useActions(catalog)
}