简体   繁体   English

Redux store.dispatch 目的

[英]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() )。

Action Creators动作创作者

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));

Logic逻辑

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.

相关问题 Store.Dispatch() 重置 Redux Store - Store.Dispatch() Resetting Redux Store Redux 中的 store.dispatch 是同步还是异步 - Is store.dispatch in Redux synchronous or asynchronous store.dispatch不是函数 - store.dispatch is not a function 'TypeError: store.dispatch is not a function' redux action testing with jest - 'TypeError: store.dispatch is not a function' redux action testing with jest 在redux中,当编写请求调度时,“ next”和“ store.dispatch”之间有什么区别? - In redux when writing thunks for dispatch, what's the difference between “next” and “store.dispatch”? 使用Redux的store.getState()和store.dispatch()进行更改而无需订阅React? - Using Redux's store.getState() and store.dispatch() to make changes without subscribing in React? 反应终极版中间件的#store.dispatch(动作)VS #next(动作) - React Redux middleware's #store.dispatch(action) vs #next(action) 当 store.dispatch(END) 出现 redux-saga 问题的下一个 js - Next js with redux-saga problem when store.dispatch(END) mapDispatchToProps与store.dispatch() - mapDispatchToProps vs. store.dispatch() 我们如何从 Redux - saga 中的 store.dispatch 返回 Promise - saga,以便我们可以等待解析然后在 SSR 中渲染? - How do we return a Promise from a store.dispatch in Redux - saga so that we can wait for the resolve and then render in SSR?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM