章节 8 - dispatch-async-action-2.js

运行之前我们在 dispatch-async-action-1.js 中实现的第一个异步 action creator:

  1. import { createStore, combineReducers } from 'redux'
  2. var reducer = combineReducers({
  3. speaker: function (state = {}, action) {
  4. console.log('speaker was called with state', state, 'and action', action)
  5. switch (action.type) {
  6. case 'SAY':
  7. return {
  8. ...state,
  9. message: action.message
  10. }
  11. default:
  12. return state;
  13. }
  14. }
  15. })
  16. var store_0 = createStore(reducer)
  17. var asyncSayActionCreator_1 = function (message) {
  18. return function (dispatch) {
  19. setTimeout(function () {
  20. dispatch({
  21. type: 'SAY',
  22. message
  23. })
  24. }, 2000)
  25. }
  26. }
  27. console.log("\n", 'Running our async action creator:', "\n")
  28. store_0.dispatch(asyncSayActionCreator_1('Hi'))
  29. // 输出:
  30. // ...
  31. // /Users/classtar/Codes/redux-tutorial/node_modules/redux/node_modules/invariant/invariant.js:51
  32. // throw error;
  33. // ^
  34. // Error: Invariant Violation: Actions must be plain objects. Use custom middleware for async actions.
  35. // ...

我们所设计的 function 似乎没有进入 reducer 函数。但是 Redux 给出了温馨提示:使用自定义中间件(middleware)来支持异步 action。看来我们的方向是正确的,可中间件(middleware)又是什么呢?

我向你保证 action creator asyncSayActionCreator_1 不仅没有问题,而且只要我们搞清楚 middleware 的概念并掌握它的使用方法,这个异步 action creator 就会按照我们所设想的结果执行。

开始下节教程:09_middleware.js