[英]Redux store.dispatch purpose
I'm reviewing a TypeScript function which performs some algorithmic logic and trying to understand where the logic actually happens我正在审查 TypeScript function 它执行一些算法逻辑并试图了解逻辑实际发生的位置
private DoSomeAlgorithmicLogic(nodeIds: any) {
const obj = CreateObject(nodeIds);
this.store.dispatch(new GetNodes(obj));
}
CreateObject
fills verious attributes of obj
and GetNodes
is merly this: CreateObject
填充了obj
的各种属性, GetNodes
是这样的:
export class GetNodes implements Action {
readonly type = GET_NODES;
constructor(public payload: any) {
}
}
I tried reading about store & dispatch here but not sure I understood the concepts presented.我尝试在这里阅读有关商店和调度的信息,但不确定我是否理解所提出的概念。
What I really need is pointers as how to further investigate/debug this functionality in order to find where the logic happens, but will be happy to get further explenation regarding app state tree and how this line relates to it (especially the parameter that should be passed to dispatch()
).我真正需要的是关于如何进一步调查/调试此功能以查找逻辑发生位置的指针,但很高兴获得有关应用程序 state 树的进一步说明以及此行与它的关系(尤其是应该是的参数传递给dispatch()
)。
It looks like whoever wrote this code got a little bit too excited by OOP and ignored the principles of Redux.看起来写这段代码的人对 OOP 有点过于兴奋,而忽略了 Redux 的原理。 An action should be serializable so using a class
as an action creator is not a good idea.动作应该是可序列化的,因此使用class
作为动作创建者不是一个好主意。
dispatch
is a function which takes an Action
. dispatch
是一个 function ,它需要一个Action
。 An Action
is an object with a type: string
property and possibly some other data.一个Action
是一个 object type: string
属性,可能还有一些其他数据。 So you can call:所以你可以打电话:
dispatch({type: MY_ACTION});
dispatch({type: MY_ACTION, payload: someData});
But typically we use action creator functions to create the action object.但通常我们使用动作创建函数来创建动作 object。 That is what this class GetNodes
is doing, but it's a bizarre way to do it.这就是class GetNodes
正在做的事情,但这是一种奇怪的方法。 It should probably just be a function:它可能应该只是一个 function:
export const getNodes = (payload: any): Action => ({
type: GET_NODES,
payload
})
I hate this payload: any
because any
really defeats the point of using Typescript.我讨厌这个payload: any
因为any
真的违背了使用 Typescript 的意义。 It should probably be something like payload: Node[]
.它可能应该类似于payload: Node[]
。
With the action creator function, you call:使用动作创建器 function,您可以调用:
dispatch(getNodes(obj));
Action creators generally don't have any logic.动作创建者通常没有任何逻辑。 They just arrange the arguments into an Action
object.他们只是将 arguments 安排到一个Action
object 中。
Redux logic happens in the reducer. Redux 逻辑发生在减速器中。 The reducer is a function that takes the current state
and the dispatched action
and returns the next state. reducer 是一个 function,它采用当前的state
和调度的action
并返回下一个 state。
const myReducer = (state: MyState, action: Action): MyState => {...
The reducer is passed as an argument when you create the store instance.当您创建商店实例时,reducer 作为参数传递。 You will either find one reducer function or a function created by calling combineReducers
with a map object of reducer functions.您将找到一个减速器 function 或一个 function 通过调用combineReducers
创建的 map2668CFDE6311B4BEBer6 函数。 Look for where this.store
is set and look for the first argument of createStore
.查找this.store
的设置位置并查找createStore
的第一个参数。 That's the reducer function where you will find all of the logic for handling various actions.这就是减速器 function ,您将在其中找到处理各种操作的所有逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.