【问题标题】:react-redux not dispatching thunk api callreact-redux 没有调度 thunk api 调用
【发布时间】:2017-08-28 11:54:09
【问题描述】:

我正在使用带有 redux 和 Api 调用的工作网络版本,并将它们移植到 React Native 应用程序。但是,我注意到在尝试发送 thunk 以进行 API 调用时,我似乎无法在我的 thunk 中看到控制台日志来确认发送。这让我觉得有些东西没有正确连接,但我只是不明白那是什么。我错过了什么?

我创建了一个初始状态的商店:当我登录 store.getState() 时,一切看起来都很好。

const initialState = {
  config: fromJS({
    apiUrl: "http://localhost:3000/account-data",
  })
}
const store = createStore(
  reducers,
  initialState,
  compose(
    applyMiddleware(thunk),
  )
)

我使用 mapDispatchToProps,我在我的道具列表中看到了这些函数

export function mapDispatchToProps(dispatch) {
  return {
    loadProducts: () => dispatch(loadProducts())
  };
}

但是,当我检查我的 loadProducts 函数时,我没有看到确认调度的控制台日志。这里发生了什么?为什么 loadProducts 没有调度?在网络版本上,我能够确认网络请求和日志。在 React Native 上,我看不到网络请求或这些控制台日志。

export function loadProductsCall() {
  console.log('in RN loadProductsCall') //don't see this
  const opts = constructAxpOpts();
  return {
    [CALL_API]: {
      types: [
        LOAD_REQUEST,
        LOAD_SUCCESS,
        LOAD_FAILURE
      ],
      callAPI: (client, state) =>
        client.get(`${state.config.get('apiUrl')}/members`, opts),
      shouldForceFetch: () => false,
      isLoaded: state => !!(state.core.resources.products.get('productsOrder') &&
        state.core.resources.products.get('productsOrder').length),
      getResourceFromState: (state) => state.core.resources.products.toJS(),
      isLoading: state => !!state.core.resources.products.get('isLoading'),
      getLoadingPromise: state => state.core.resources.products.get('loadingPromise'),
      payload: {}
    }
  };
}

export function loadProducts() {
  console.log('in loadProducts') //don't see this
  return (dispatch) =>
  console.log('in loadProducts dispatched 2') //don't see this either 
    dispatch(loadProductsCall())
      .then((response) => {
        return response;
      });
}

【问题讨论】:

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


    【解决方案1】:

    此代码缺少处理三种操作类型的自定义 API 中间件。此外,在mapDispatchToProps 中,一个函数正在包装调度。该函数需要被解包并返回一个承诺或在代码中的其他地方调用。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2019-11-12
      • 2021-04-05
      • 2020-08-18
      • 2018-05-21
      • 2016-12-11
      • 2018-06-25
      • 1970-01-01
      • 2016-12-11
      相关资源
      最近更新 更多