Why Curry Redux Middleware: state => next => action {} vs. (state, next) => action {}

北战南征 提交于 2020-01-05 11:48:10

问题


After reading both Redux's documentation on middleware and source code on applyMiddleware, I don't understand why middleware need the curry syntax:

const logger = store => next => action => {
  console.log('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  return result
}

Couldn't the same thing be achieved by doing

const logger = (store, next) => action => {
  console.log('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  return result
}

Making the compose call in applyMiddleware:

dispatch = compose(...middleware)(middlewareAPI , store.dispatch)

回答1:


A discussion with Dan Abramov can be found here regarding this. He said,

We could have made it (store, next) => action => () but I don't see a problem with just going all the way. You might want some configuration later, at which point options => (store, next) => action => () looks kinda arbitrary.

So no, it is not necessary to curry the parameters.




回答2:


No, because it a way to defer an function to be executed later. ()=>() returns a function object which only get executed later when the func obj is called.



来源:https://stackoverflow.com/questions/46730137/why-curry-redux-middleware-state-next-action-vs-state-next-acti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!