How to divide the logic between Redux reducers and action creators?

后端 未结 3 727
無奈伤痛
無奈伤痛 2020-12-08 00:57

I have some logic that I\'ve put in the reducer which I\'m thinking should be possibly put in the Action and passed down?

Is it best practice to put this sort of stu

3条回答
  •  半阙折子戏
    2020-12-08 01:17

    My understanding is that actions should be simple objects that contain two things: (i) the action type and (ii) what changed (i.e. the new data).

    Reducers on the other hand, are pure functions that take actions and the previous app state as inputs and return the new app state. How they accomplish this is up to you. You can add whatever logic necessary to take the combination of previous state + action and return the new state as long as you don't mutate data outside your reducer function(s).

    As for your code specifically, I'm not sure the deal() function belongs in either an action or a reducer. I think a better place might be in some sort of event handler (e.g. onClick). You could then pass the results of the deal call as an action to your reducer.

提交回复
热议问题