goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

(Help needed) Alt's equivalent of Redux's combineReducer #683

Closed xxRockOnxx closed 7 years ago

xxRockOnxx commented 7 years ago

I saw in the docs that Alt can do reducers but it is not clear how to do multiple one.

reduce is a method of a store that acceps: reduce(state, { action, data }): {}

How can I do the following in Alt?


const reducerOne = (state={}, action) => {
  switch here
  return state;
}

const reducerTwo = (state={}, action) => {
  switch here
  return state;
}

const reducers = combineReducers({
  reducerOne, reducerTwo
})

const store = createStore(reducers)

On the above example, we made a single using all the reducers we need.

On Alt how do we do this? As to how I understand this, reduce method works like

class StoreA{

  reduce(state={}, action){
    switch here too maybe?
    return state
  }
}

how do we combine multiple stores or reducers? Please provide an example.

goatslacker commented 7 years ago

There's no need to "combine" the reducers. The reason why redux combines them is because there is only a single store. In Alt you can have as many stores as you want...so just create another reducer like:

class StoreB {
  reduce() { }
}
alt.createStore(StoreB)
xxRockOnxx commented 7 years ago

But what's the point in reducing it at all then? The point of Redux's reducer is to have a singe source of truth.

It's either that's not alt's use of its reducer OR we put all the reducer in one place in alt compared to Redux that splitting it.

Which one? Or is there anything I don't understand here?

Is it only there so it can listen to all the dispatch? Then what's the difference on binding it to all actions? That'll be equal or at least similar.

goatslacker commented 7 years ago

Regardless you still have a single source of truth with Alt. It's still a single tree as well all listed under alt.stores.

The point of reducing is so yes you can listen to all dispatches. It also has a different interface which makes porting to/from redux more palatable.

The only other way you can listen to all actions is by using a lifecycle method I think.

You can sort of map redux's reducers to alt stores 1:1 except you don't have to combineStores.

xxRockOnxx commented 7 years ago

Ok, I got a bit lost the last time I asked, but now I think I get it.

The Store in alt is the equivalent of Redux's main Store I presume? It's just that in Redux, reducers are split to make it more "organized"?

If the answer is yes, well then, I can make it also on Alt by making an objects maybe as reducers and use those inside the reduce() method.

This is the line in the docs that made it confusing.

This way you can write your stores as reducers of dispatches.

As to how I understand that line, that made me think that each store are the "reducers" and there would be a function that has an equivalent of combineReducers to make it one.

Again, did I understand what you mean?

goatslacker commented 7 years ago

A store in alt is like a reducer in redux.

alt.createStore(FooStore, 'FooStore') // <-- the name you pass in here, that's how you "combine reducers"

so then your data is available under alt.stores.FooStore.

xxRockOnxx commented 7 years ago

Hmm

Alt Store = Redux reducer. - got it.

---- EDIT ----

ok, so tried doing

and wasn't the expected result. Hmm, i think i didn't get what you mean by i'll keep on fiddling around until i got a reply from you.

Worth mentioning that I actually do alt.createStore(immutable(FooStore),'AllStores')

goatslacker commented 7 years ago
alt.createStore(FooStore, 'FooStore')
alt.createStore(BarStore, 'BarStore')
alt.createStore(BazStore, 'BazStore')

alt.stores // `state` in Redux.
alt.stores.FooStore
alt.stores.BarStore
alt.stores.BazStore

If you're looking for a top-level reduce function that'll return the entire state then what you can do is dispatch.

xxRockOnxx commented 7 years ago

Yes, finally get it now. Thanks btw. Just confused.