Conditions in reducers

2019-02-28 08:11发布

Is it a bad idea to use conditions in reducers? Something like this:

case types.SET_SOME_ACTION:
   if(typeof someElement !== "undefined") {
      return Object.assign({}, state, {
          someElement: action.value
       })
    }

Trying to see if this is an anti-pattern. Thanks.

1条回答
我想做一个坏孩纸
2楼-- · 2019-02-28 08:41

I believe it is a general consensus to put your business logic in an action creator. That leaves reducers with only one responsibility: to update the state.

See this discussion for more: https://github.com/reactjs/redux/issues/1165

The redux FAQ also recommends a dumb reducer: http://redux.js.org/docs/faq/CodeStructure.html#how-should-i-split-my-logic-between-reducers-and-action-creators-where-should-my-business-logic-go

Personally, I only do simple validation in the redux store, the actual business logic is handled by Redux Observable or Redux Thunk.

查看更多
登录 后发表回答