unistore icon indicating copy to clipboard operation
unistore copied to clipboard

combineReducers

Open KovalevAnton opened this issue 8 years ago • 3 comments

Hello! Tell me, please, how i can join reducers, like "combineReducers" in Redux?

KovalevAnton avatar Jan 23 '18 11:01 KovalevAnton

Hmm - do you have an example of where this is needed?

function combineReducers(...fns) {
  return (...args) => Object.assign(...fns.map( fn => fn(...args) );
}

developit avatar Jan 25 '18 15:01 developit

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 avatar Jun 12 '18 05:06 aprilmintacpineda

@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)
}

dy avatar Apr 03 '19 22:04 dy