Redux中间件的返回值

时间:2018-09-10 03:17:52

标签: javascript reactjs redux

Redux指南suggests指出next(action)应该从中间件函数中返回,但并未明确记录中间件API。

这里是React + Redux example

const fooMiddleware = store => next => action => {
  return next({ ...action, foo: 'foo' });
};

const foobarMiddleware = store => next => action => {
  return next({ ...action, foobar: `${action.foo}bar` });
};

...

const store = createStore(
  reducer,
  applyMiddleware(fooMiddleware, foobarMiddleware)
);

无论中间件中的return为何,它似乎都能按预期工作。

Redux中间件函数中是否需要返回值,为什么?它到底如何影响Redux应用程序?如果缺少它可以破坏事情,我将不胜感激地举一个说明问题的具体示例。上面的演示是否有可能被破坏?

1 个答案:

答案 0 :(得分:2)

因为中间件是一系列功能,其中每个中间件 function action 分配给下一个 function >在链中,每个中间件 function 都需要一个调度 function 。调用组合函数时,中间件函数接收这些调度函数,第n个中间件函数接收<div class="mainBox"> <div id="checkLabel" class="labelClass"> <p for="improvements">Things that should be improved in the future (Check all that apply):</p> </div> <div id="checkbox" class="rightTab"> <label><input type="checkbox" name="improvements" value="parking"> Parking</label> <label><input type="checkbox" name="improvements" value="bathrooms"> Bathrooms</label> <label><input type="checkbox" name="improvements" value="amenities"> Amenities</label> <label><input type="checkbox" name="improvements" value="wifi"> Faster Wi-Fi <label><input type="checkbox" name="improvements" value="Accommodations"> Accommodations</label> <label><input type="checkbox" name="improvements" value="housekeeping"> Daily Housekeeping</label> <label><input type="checkbox" name="improvements" value="furniture"> Furniture and Linens</label> <label><input type="checkbox" name="improvements" value="checkin&out"> Rolling Check-in/out</label> <label><input type="checkbox" name="improvements" value="cs"> 24/7 Customer Service</label> <label><input type="checkbox" name="improvements" value="planning"> Hotel Planning & Bookings</label> </div> </div>,随后的每个 function 都接收前一个中间件函数作为其调度。

中间件只是具有签名的功能

store.dispatch