Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

javascript - 在动作创建者中访问Redux状态?(Accessing Redux state in an action creator?)

Say I have the following:(说我有以下内容:)

export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return { type: SOME_ACTION, } } And in that action creator, I want to access the global store state (all reducers).(在该动作创建者中,我想访问全局存储状态(所有reducers)。) Is it better to do this:(这样做更好:) import store from '../store'; export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return { type: SOME_ACTION, items: store.getState().otherReducer.items, } } or this:(或这个:) export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return (dispatch, getState) => { const {items} = getState().otherReducer; dispatch(anotherAction(items)); } }   ask by ffxsam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are differing opinions on whether accessing state in action creators is a good idea:(关于在动作创作者中访问状态是否是一个好主意,有不同的意见:)

Redux creator Dan Abramov feels that it should be limited: "The few use cases where I think it's acceptable is for checking cached data before you make a request, or for checking whether you are authenticated (in other words, doing a conditional dispatch). I think that passing data such as state.something.items in an action creator is definitely an anti-pattern and is discouraged because it obscured the change history: if there is a bug and items are incorrect, it is hard to trace where those incorrect values come from because they are already part of the action, rather than directly computed by a reducer in response to an action. So do this with care."(Redux创建者Dan Abramov觉得应该受到限制:“我认为可以接受的少数用例是在您提出请求之前检查缓存数据,或者检查您是否经过身份验证(换句话说,进行条件调度)。我认为,通过数据state.something.items在行动的创建者绝对是一个反模式和气馁,因为它掩盖了改变历史:如果有一个错误, items是不正确的,这是很难追查那些不正确值来自于因为它们已经是动作的一部分,而不是由减速器直接计算以响应动作。所以要小心这样做。“) Current Redux maintainer Mark Erikson says it's fine and even encouraged to use getState in thunks - that's why it exists .(目前的Redux维护者Mark Erikson说它很好,甚至鼓励在thunk中使用getState - 这就是它存在的原因 。) He discusses the pros and cons of accessing state in action creators in his blog post Idiomatic Redux: Thoughts on Thunks, Sagas, Abstraction, and Reusability .(他在他的博客文章Idiomatic Redux中讨论了在动作创作者中访问状态的利弊:关于Thunks,Sagas,Abstraction和Reusability的思考 。) If you find that you need this, both approaches you suggested are fine.(如果您发现需要这个,那么您建议的两种方法都可以。) The first approach does not require any middleware:(第一种方法不需要任何中间件:) import store from '../store'; export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return { type: SOME_ACTION, items: store.getState().otherReducer.items, } } However you can see that it relies on store being a singleton exported from some module.(但是你可以看到它依赖于store是从某个模块导出的单例。) We don't recommend that because it makes it much harder to add server rendering to your app because in most cases on the server you'll want to have a separate store per request .(我们不建议这样做,因为它使服务器渲染添加到您的应用程序更加困难,因为在大多数情况下,在服务器上,您希望每个请求都有一个单独的存储 。) So while technically this approach works, we don't recommend exporting a store from a module.(因此,虽然从技术上讲这种方法有效,但我们不建议从模块中导出商店。) This is why we recommend the second approach:(这就是我们推荐第二种方法的原因:) export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return (dispatch, getState) => { const {items} = getState().otherReducer; dispatch(anotherAction(items)); } } It would require you to use Redux Thunk middleware but it works fine both on the client and on the server.(它需要您使用Redux Thunk中间件,但它在客户端和服务器上都可以正常工作。) You can read more about Redux Thunk and why it's necessary in this case here .(您可以在此处阅读有关Redux Thunk的更多信息以及为什么需要它。) Ideally, your actions should not be “fat” and should contain as little information as possible, but you should feel free to do what works best for you in your own application.(理想情况下,您的行为不应该“胖”,并且应该包含尽可能少的信息,但您应该随意在自己的应用程序中做最适合您的操作。) The Redux FAQ has information on splitting logic between action creators and reducers and times when it may be useful to use getState in an action creator .(Redux FAQ包含有关在动作创建者和减少者之间拆分逻辑的信息, 以及 在动作创建者中使用getState可能有用的时间 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...