admin管理员组文章数量:1278983
There seems to be agreement that if an action creator needs state information (and we want to be independent of state shape), the ponent calling the action should submit the required state slices to the action. See e.g. Dan Abramov's ment here
But why are we not instead importing selectors into actions?
import { mySelector } from '../reducers';
const myAction = () => (dispatch, getState) => {
const requiredState = mySelector(getState());
etc...
};
It looks like this would save at least some state slices a roundtrip through ponents and de-couple things.
What's the disadvantage of doing this? (Except perhaps that actions cannot export ActionTypes
.)
There seems to be agreement that if an action creator needs state information (and we want to be independent of state shape), the ponent calling the action should submit the required state slices to the action. See e.g. Dan Abramov's ment here
But why are we not instead importing selectors into actions?
import { mySelector } from '../reducers';
const myAction = () => (dispatch, getState) => {
const requiredState = mySelector(getState());
etc...
};
It looks like this would save at least some state slices a roundtrip through ponents and de-couple things.
What's the disadvantage of doing this? (Except perhaps that actions cannot export ActionTypes
.)
2 Answers
Reset to default 9Yes, if you are going to access the store state in your action creators, thunks, or sagas, then you should use selector functions to encapsulate the lookup process.
The other aspect of your question is if accessing the store state in an action creator is a good idea. Dan has some reservations about it, and I understand where he's ing from, but from my point of view it's fine. I wrote a blog post that discusses a number of mon concerns about use of thunks, sagas, and state called Idiomatic Redux: Thoughts on Thunks, Sagas, Abstraction, and Reusability, and gave my reasons why I feel that those concerns are not generally real problems to worry about.
AFAIK the idea in the ment you mentioned is to "decouple" your action from state.
Decoupling the actions from the application state taking in the data you need from where the actions are called make the actions not bounded to state.
Usually decoupling is always a good thing, some advantages:
- Easier to maintain code and change implementations
- Easier unit test.
- Less dependencies for your actions.
本文标签: javascriptRedux importing selectors into actionsStack Overflow
版权声明:本文标题:javascript - Redux: importing selectors into actions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741220162a2360817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论