简体   繁体   English

redux thunk function 是如何运作的? 当调用 redux thunk function 时会发生什么?

[英]How does a redux thunk function operate? What happens when a redux thunk function gets called?

I am struggling to wrap my head around the syntax/ understanding how the thunk function flow...我正在努力思考语法/理解 thunk function 是如何流动的......
ie the following function:即以下 function:

function incrementAsync() {
  return dispatch => {
    setTimeout(() => {
      dispatch(increment());
    }, 1000);
  };
}

I think I mostly am confused by the passing in dispatch as an argument part.我想我主要对将dispatch作为参数部分传递感到困惑。 What exactly happens when incrementAsync is called?调用incrementAsync时到底发生了什么?

A normal Redux action is a plain JS object, as illustrated in the tutorial: https://redux.js.org/basics/actions正常的 Redux 操作是一个普通的 JS object,如教程中所示: https://redux.js.org/basics/actions

What Thunk does is to extend Redux so that actions can also be a function. When incrementAsync is called, it returns a new function that takes an argument dispatch . Thunk 所做的是扩展 Redux,这样 actions 也可以是 function。当调用incrementAsync时,它返回一个带有参数dispatch的新 function。 That function is then called by Redux (via Thunk), passing in the dispatch() function from Redux as an argument, enabling you to dispatch more actions (a)synchronously.然后 883410040658988 被 Redux(通过 Thunk)调用,从 Redux 传入dispatch() function 作为参数,使您能够同步调度更多操作 (a)。

This is all taking advantage of the fact that JS functions are first-class members of the language and can be assigned to a variable, and passed around.这一切都利用了 JS 函数是语言的一等成员并且可以分配给变量并传递的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM