【问题标题】:Redux action not getting called the second timeRedux 操作第二次没有被调用
【发布时间】:2017-02-26 15:23:15
【问题描述】:

我有两个 redux 操作,调用如下。

export function action1(params) {
  //This line is always called.
  return (dispatch) => {
    //This line is not called the second time.
    return MyApi.call1(params)
    .then(response => {
      // some logic
      return dispatch(someFunction1());
    })
    .catch(error => {
      throw(error);
    });
  };
}

export function action2(params) {
  return (dispatch) => {
    return MyApi.call2(params)
    .then(response => {
      // call the first API again
      action1();
      return dispatch(someFunction2());
    })
    .catch(error => {
      throw(error);
    });
  };
}

当第一次加载视图时,action1 在视图的构造函数中被调用。在同一视图中执行操作并触发action2 时,需要在action2 成功时调用action1 以从服务器获取更新的列表。不幸的是,当第二次调用action1 时,代码会中断而没有任何错误。

我在这里错过了什么?

【问题讨论】:

  • 顺便说一句,你还没有派出 action1 ,比如:dispatch(action1(params)) ;
  • 成功了。我真是太傻了。您可以将此作为答案发布以便我接受吗?

标签: javascript reactjs react-native redux react-redux


【解决方案1】:

您还没有发送action1

dispatch( action1( params ) )

在没有dispatch 的情况下调用action1() 只会返回一个函数。为了在返回的函数中得到dispatch,你应该dispatch那个函数。然后会被redux-thunk中间件捕获。中间件将传递dispatch 并调用函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2020-08-18
    • 2016-08-14
    • 1970-01-01
    • 2016-06-14
    • 2015-03-17
    相关资源
    最近更新 更多