Return 来自 React Redux Thunk 的承诺

Return promise from React Redux Thunk

我有下一个React/Redux/Thunk代码:

这是我给 API 的电话:

//api.js
export const categoriesFetchData = (page, parentOf) => {
  return axios.get(
    url +
    'categories/' +
    '?parent=' +
    parentOf +
    '&limit=10' +
    '&offset=' +
    (page - 1) * 10,
  );
};

这是我的操作(我假装 return 来自此操作的 axios 承诺):

//actions.js
export const subCategoriesFetchData = (page = 1, parentOf) => {
  return dispatch => {
    dispatch(oneCategoryLoading(true));
    return api.categoriesFetchData(page, parentOf)
      .then(response => {
        dispatch(subCategoriesFetchDataSuccess(response.data.results));
        dispatch(oneCategoryLoading(false));
      })
      .catch(error => {
        console.log(error);
      });
  };
};

在我的容器中:

const mapDispatchToProps = dispatch => {
  return {
    fetchOneCategory: slug => {
      dispatch(fetchOneCategory(slug)).then(() => {
        console.log('working');
      });
    },

  };
};

但是我得到这个错误:

Uncaught TypeError: Cannot read property 'then' of undefined

有什么问题以及如何 return 容器中的承诺?

感谢您的帮助。

在您的容器中,您不需要

.then(() => {
    console.log('working');
  });

dispatch(fetchOneCategory(slug))没有return承诺,没有then可读

这是我处理这个问题的方法。

首先,您需要对 subCategoriesFetchData 函数进行一些更改:

export const subCategoriesFetchData = (page = 1, parentOf) => {
  return dispatch => {
    dispatch(oneCategoryLoading(true));

    // That's the key thing. Return a new promise, then ma
    return new Promise((resolve, reject) => {

      api.categoriesFetchData(page, parentOf)
        .then(response => {
          dispatch(subCategoriesFetchDataSuccess(response.data.results));
          dispatch(oneCategoryLoading(false));

          resolve(response); // Resolve it with whatever you need
        })
        .catch(error => {
          console.log(error);

          reject(error); // And it's good practice to reject it on error (optional)
        });
    });
  };
};

然后,下面是如何使用 mapDispatchToProps 然后链接 .then()s:

import React from 'react';
import { connect } from 'react-redux';
import { subCategoriesFetchData } from './wherever-it-is';

class MyComponent extends React.Component {

  componentDidMount() {
    this.props.subCategoriesFetchData()
      .then( () => { console.log('it works'); })
      .catch( () => { console.log('on error'); });
  }

  render() {
    return ( <p>Whatever</p> );
  }
}

const mapDispatchToProps = { subCategoriesFetchData };

connect(null, mapDispatchToProps)(MyComponent);

抱歉,我会回答我自己的问题:

我改变这个:

const mapDispatchToProps = dispatch => {
  return {
    fetchOneCategory: slug => {
      dispatch(fetchOneCategory(slug)).then(() => {
        console.log('working');
      });
    },

  };
};

const mapDispatchToProps = dispatch => {
  return {
    fetchOneCategory: slug => {
      return dispatch(fetchOneCategory(slug))
    },
  };
};

现在我可以做到了:

import React from 'react';
import { connect } from 'react-redux';

class MyComponent extends React.Component {

  componentDidMount() {
    this.props.fetchOneCategory()
      .then( () => { console.log('it works'); })
      .catch( () => { console.log('on error'); });
  }

  render() {
    return ( <p>Whatever</p> );
  }
}

感谢您的帮助!