React-native Redux 业务逻辑应该在 actions 还是 reducer 里面
React-native Redux Should business logic be inside actions or reducers
我已经开始致力于 reactnative - redux 项目。我对这种功能范式完全陌生。我的问题很简单:我有不同的 login/signup 选项,其中之一是 facebook。
在我的操作文件中,我从 facebook 获取令牌。我应该将它发送到服务器进行检查。此请求可以 return 多个结果
- 该用户是新用户,打开新用户页面
- 该用户已存在并已批准,打开申请页面
- 该用户已存在,但尚未通过短信验证,打开短信验证界面。
问题是;我应该把这些逻辑放在哪里?我应该在操作上完成所有操作还是只将事件发送到 reducer 并让它决定。我对此感到困惑。
谢谢
根据 Redux FAQ entry on "where should my business logic live?":
There's no single clear answer to exactly what pieces of logic should go in a reducer or an action creator. Some developers prefer to have “fat” action creators, with “thin” reducers that simply take the data in an action and blindly merge it into the corresponding state. Others try to emphasize keeping actions as small as possible, and minimize the usage of getState() in an action creator. (For purposes of this question, other async approaches such as sagas and observables fall in the "action creator" category.)
This comment sums up the dichotomy nicely:
Now, the problem is what to put in the action creator and what in the reducer, the choice between fat and thin action objects. If you put all the logic in the action creator, you end up with fat action objects that basically declare the updates to the state. Reducers become pure, dumb, add-this, remove that, update these functions. They will be easy to compose. But not much of your business logic will be there. If you put more logic in the reducer, you end up with nice, thin action objects, most of your data logic in one place, but your reducers are harder to compose since you might need info from other branches. You end up with large reducers or reducers that take additional arguments from higher up in the state.
我还在我的博客中讨论了“厚”和“薄”减速器的想法 post The Tao of Redux, Part 2 - Practice and Philosophy。
我已经开始致力于 reactnative - redux 项目。我对这种功能范式完全陌生。我的问题很简单:我有不同的 login/signup 选项,其中之一是 facebook。 在我的操作文件中,我从 facebook 获取令牌。我应该将它发送到服务器进行检查。此请求可以 return 多个结果
- 该用户是新用户,打开新用户页面
- 该用户已存在并已批准,打开申请页面
- 该用户已存在,但尚未通过短信验证,打开短信验证界面。
问题是;我应该把这些逻辑放在哪里?我应该在操作上完成所有操作还是只将事件发送到 reducer 并让它决定。我对此感到困惑。
谢谢
根据 Redux FAQ entry on "where should my business logic live?":
There's no single clear answer to exactly what pieces of logic should go in a reducer or an action creator. Some developers prefer to have “fat” action creators, with “thin” reducers that simply take the data in an action and blindly merge it into the corresponding state. Others try to emphasize keeping actions as small as possible, and minimize the usage of getState() in an action creator. (For purposes of this question, other async approaches such as sagas and observables fall in the "action creator" category.)
This comment sums up the dichotomy nicely:
Now, the problem is what to put in the action creator and what in the reducer, the choice between fat and thin action objects. If you put all the logic in the action creator, you end up with fat action objects that basically declare the updates to the state. Reducers become pure, dumb, add-this, remove that, update these functions. They will be easy to compose. But not much of your business logic will be there. If you put more logic in the reducer, you end up with nice, thin action objects, most of your data logic in one place, but your reducers are harder to compose since you might need info from other branches. You end up with large reducers or reducers that take additional arguments from higher up in the state.
我还在我的博客中讨论了“厚”和“薄”减速器的想法 post The Tao of Redux, Part 2 - Practice and Philosophy。