为什么 Redux 中间件定义为三个箭头函数而不是一个带有三个参数的函数? [英] Why Redux middleware defined as three arrow functions instead of a single function with three arguments?

查看:93
本文介绍了为什么 Redux 中间件定义为三个箭头函数而不是一个带有三个参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为什么要这样做:

const middleware = store => next => action => { ... }

而不是像

const middleware = (store, next, action) => { ... }

以这种方式设计中间件对 Redux 团队有什么好处吗?如果我们拆分中间件功能,是否可以利用一些功能?

Was there some advantage to the Redux team for designing middleware this way? Is there some functionality we can take advantage of if we split up the middleware functions?

在我自己的应用程序中,我定义了一个 simpleMiddleware() 函数,该函数可以从第二种形式转换为第一种形式,而且似乎运行良好.

In my own applications I've defined a simplerMiddleware() function that can convert from the second form to the first and it seems to work well.

function simpleMiddleware(simpleMiddlewareFunction) {
  return store => next => action => simpleMiddlewareFunction(store, next, action);
}

为什么是三个箭头函数?

Why the three arrow functions?

注意:我不是在问什么是柯里化,或者为什么在函数式编程中存在柯里化,或者柯里化的一般好处是什么;Redux 设计者选择这个签名而不是更简单的三参数函数是否有特定的原因?

Note: I'm not asking what is currying or why currying exists in functional programming or what are the generic benefits of currying; was there a specific reason the Redux designers chose this signature over a simpler three argument function?

推荐答案

这在 关于为什么中间件签名使用柯里化?"的 Redux FAQ 条目::

Redux 中间件是使用类似于 const middleware = storeAPI => 的三重嵌套函数结构编写的.下一个 =>动作 =>{},而不是看起来像 const middleware = (storeAPI, next, action) => 的单个函数{}.这有几个原因.

Redux middleware are written using a triply-nested function structure that looks like const middleware = storeAPI => next => action => {}, rather than a single function that looks like const middleware = (storeAPI, next, action) => {}. There's a few reasons for this.

一个是柯里化"函数是一种标准的函数式编程技术,Redux 明确打算在其设计中使用函数式编程原则.另一个是柯里化函数创建了闭包,您可以在其中声明在中间件的生命周期内存在的变量(可以将其视为与在类实例的生命周期内存在的实例变量等效的功能).最后,这只是 Redux 最初设计时选择的方法.

One is that "currying" functions is a standard functional programming technique, and Redux was explicitly intended to use functional programming principles in its design. Another is that currying functions creates closures where you can declare variables that exist for the lifetime of the middleware (which could be considered a functional equivalent to instance variables that exist for the lifetime of a class instance). Finally, it's simply the approach that was chosen when Redux was initially designed.

声明中间件的柯里化函数签名被一些人认为是不必要的,因为在执行 applyMiddleware 函数时 store 和 next 都可用.此问题已被确定不值得引入重大更改,因为 Redux 生态系统中现在有数百个中间件依赖于现有的中间件定义.

The curried function signature of declaring middleware is deemed unnecessary by some, because both store and next are available when the applyMiddleware function is executed. This issue has been determined to not be worth introducing breaking changes, as there are now hundreds of middleware in the Redux ecosystem that rely on the existing middleware definition.

这篇关于为什么 Redux 中间件定义为三个箭头函数而不是一个带有三个参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆