Skip to content

RFC: Simplify middleware signature #1744

Closed
@gaearon

Description

@gaearon

This was proposed numerous times before (#784, happypoulp/redux-tutorial#48), and I still hear about the Redux “obnoxious” middleware signature. I think the complaint comes from the fact that most middleware never uses store argument so it’s far from being clear why it’s required.

I’m not a big fan of changing things, but we have barely made any breaking changes since the release, and since we have an overhaul for store enhancers in #1702, I realized that if I was working on Redux 1.0 today, I’d keep the middleware signature simpler. I’m happy to release 4.0 alpha and keep it as next version for several months to let middleware authors catch up.

The signature I’m proposing is:

const logger = (next) => (action) => {
  // ...
}

Note that most middleware only care about the next argument so this is why it should be the first rather than store. You can then get store if you need to read the state or dispatch from the beginning of the chain—both are advanced use cases:

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

As a migration strategy, I suggest a compat library:

import { createStore, applyMiddleware } from 'redux'
import { wrapCompatMiddleware } from 'redux-compat'

const store = createStore(
  reducer,
  applyMiddleware(
    thunk, // new-style middleware
    wrapCompatMiddleware(logger), // middleware that hasn't been updated yet
  )
)

This will let consumers jump into 4.x right away but since they’d have to add a tiny compat package, that would put some pressure onto middleware authors to update their middleware for 4.x.

Finally, we can avoid doing this at all. However I don’t see another round of breaking changes like 4.x, so this is the only opportunity where I’m ready to do it. I’ve been telling people for months that “it’s just currying” but I can’t give a compelling reason for it other than “we just happened to choose this style”. And I do think this style is confusing to beginners, just like the original style of applying enhancers was. I think we improved it in 3.1.0, and I think we can improve this as well.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions