react-simply icon indicating copy to clipboard operation
react-simply copied to clipboard

Thought on the Reducer

Open MikeMuxika opened this issue 6 years ago • 1 comments

Wondering what you think about doing this for your reducer instead of the switch statement? My thoughts were switch statements could get long, plus maintaining a large one could be annoying. Plus the type key/value pair seems redundant if you make the added state object agnostic of static state names.

const reducer = (state, action) => {
  const actionKey = Object.getOwnPropertyNames(action)
  return action ? { ...state, [actionKey] : action[actionKey] } : state
}

instead of dispatching like so,

() => dispatch({ type: 'type', thing: false})

you would dispatch like so,

() => dispatch({thing: false})

MikeMuxika avatar Mar 28 '19 23:03 MikeMuxika

Reducer and action can be written in any way in your app. Of course your solution can work too. Also depends on the use case. If the action passes simple value, your solution is enough. But usually you need to do more complex operations with your state (merge deeper objects, etc.)

lukashala avatar May 15 '19 08:05 lukashala