为什么 Redux 中间件定义为三个箭头函数而不是一个具有三个参数的函数?
Why Redux middleware defined as three arrow functions instead of a single function with three arguments?
我们为什么这样做:
const middleware = store => next => action => { ... }
而不是像
这样简单的东西
const middleware = (store, next, action) => { ... }
Redux 团队以这种方式设计中间件是否有一些优势?如果我们拆分中间件功能,是否有一些我们可以利用的功能?
在我自己的应用程序中,我定义了一个 simplerMiddleware()
函数,它可以从第二种形式转换为第一种形式,而且它似乎运行良好。
function simpleMiddleware(simpleMiddlewareFunction) {
return store => next => action => simpleMiddlewareFunction(store, next, action);
}
为什么是三个箭头函数?
注意:我不是在问什么是柯里化,或者为什么柯里化存在于函数式编程中,或者柯里化的一般好处是什么; Redux 设计者选择此签名而不是更简单的三参数函数是否有特定原因?
这在 the Redux FAQ entry on "why does the middleware signature use currying?" 中有具体说明:
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.
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.
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.
我们为什么这样做:
const middleware = store => next => action => { ... }
而不是像
这样简单的东西const middleware = (store, next, action) => { ... }
Redux 团队以这种方式设计中间件是否有一些优势?如果我们拆分中间件功能,是否有一些我们可以利用的功能?
在我自己的应用程序中,我定义了一个 simplerMiddleware()
函数,它可以从第二种形式转换为第一种形式,而且它似乎运行良好。
function simpleMiddleware(simpleMiddlewareFunction) {
return store => next => action => simpleMiddlewareFunction(store, next, action);
}
为什么是三个箭头函数?
注意:我不是在问什么是柯里化,或者为什么柯里化存在于函数式编程中,或者柯里化的一般好处是什么; Redux 设计者选择此签名而不是更简单的三参数函数是否有特定原因?
这在 the Redux FAQ entry on "why does the middleware signature use currying?" 中有具体说明:
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 likeconst middleware = (storeAPI, next, action) => {}
. There's a few reasons for this.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.
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.