Solving Redux’s shortcoming in 150 LOCs
Matt Krick
1572

Hi Matt,

What do you think of the idea of just putting the relevant subpath of the redux state in the action meta? In my experience it solves at least #1, #2, and #5 very very easily, without the need for any special framework. It’s not just one reducer for two states, it’s one action creator, and other reducers (like number of times any counter has been incremented) don’t need to listen to a continuously-expanding list of action types.

{

type: INCREMENT,

meta: {
reduxPath: [‘a’]
}
}
{
type: INCREMENT,
meta: {
reduxPath: [‘b’]
}
}

(Using immutable.js state):

const incrementReducer = (state, action) => action.type === INCREMENT ? state.updateIn(action.meta.reduxPath, value => value + 1) : state

const numIncrementsReducer = reduxPath => (state, action) =>

action.type === INCREMENT ? state.updateIn(reduxPath, count => count + 1) : state

Like what you read? Give Andy Edwards a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.