slorber / scalable-frontend-with-elm-or-redux

An attempt to make Redux and Elm applications scale
http://sebastienlorber.com/
MIT License
361 stars 29 forks source link

High order reducer solution #8

Open jarvisaoieong opened 8 years ago

jarvisaoieong commented 8 years ago

This is live demo: http://jarvisaoieong.github.io/redux-architecture-challenge/ This is my original repo: https://github.com/jarvisaoieong/redux-architecture-challenge

The most valuable part of my solution is in mainReducer

Imagine, the requirement changed. Now we only want to count the NEW_GIF action happened in randomGifPair and randomGifPairOfPair, except in randomGif and randomGifList.

With high order reducer function, it is very easy to implement. We just need to call reducer enhancer to our target reducer. That's all.

const mainReducer = combineReducers({
  counter,
  button,
  randomGif,
  randomGifPair: newGifCountHor('action.type', randomGifPair),
  randomGifPairOfPair: newGifCountHor('action.action.type', randomGifPairOfPair),
  randomGifList,
});

IMO, modern frontend architecture is based on FP.

What we need to do is Inventing on Principle

My principle:

  1. immutable data
  2. pure function
  3. high order function

Apply these basis functional programming technique, we don't need any extra concept.

tomsdev commented 8 years ago

Thanks for submitting your solution @jarvisaoieong!

I quite like it but in my opinion 'action.action.type' makes the root/parent reducer know too much about the inner implementation of randomGifPairOfPair (e.g. the exact nesting level of the inner randomGif components).

I can imagine multiple ways of solving that:

What do you think?

tomkis commented 8 years ago

@jarvisaoieong I really like your approach and it's very similar to what I did with redux-elm

Just one nitpick but the code does not meet this requirement:

It should also be easy to move the position of components. For example imagine the button is top left of your app, and the business now wants it inside a popup, bottom right: this move of component in the tree should rather be easy to make (ie without having to modify all parent components, for example).

Since you need to explicitly define action nesting