Do I always need to return a value from a redux middleware?

后端 未结 1 1463
[愿得一人]
[愿得一人] 2021-01-04 06:16

In the examples given in the redux docs, there always seems to be something being returned from middlewares. However, when I call next(action) and return nothin

相关标签:
1条回答
  • 2021-01-04 06:40

    I actually tweeted about this just the other day.

    The store.dispatch() method by default returns the action that was passed in. Since the middleware pipeline wraps around dispatch(), each middleware can alter the value being passed through the pipeline on the way in, and alter the return value being passed back.

    Many middlewares rely on either being able to return a value themselves, or using the return value coming back. For example, redux-thunk returns whatever you return from your thunk function, and this is commonly used to be able to return a promise from a thunk so that you can chain dispatch(somePromiseThunk()).then( () => {}).

    If a middleware calls next(action) but doesn't actually return the value from next(), it will potentially break those capabilities. The specific behavior will depend on what middleware you have set up, and how you have them ordered.

    Because of this, the "correct" practice is that every Redux middleware should return next(action) by default unless it explicitly wants to change the return value behavior. That ensures the best compatibility and ability to compose middlewares together.

    0 讨论(0)
提交回复
热议问题