首页 文章

从React Redux Thunk返回承诺

提问于
浏览
0

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

这是我对API的调用:

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

这是我的行动(我假装从这个行动中返回一个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');
      });
    },

  };
};

但我得到这个错误:

未捕获的TypeError:无法读取未定义的属性'then'

有什么问题以及如何在容器中返回承诺?

谢谢你的帮助 .

3 回答

  • 0

    这是我如何接近这一点 .

    首先,您需要对 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() 来做的伎俩:

    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);
    
  • 0

    在你的容器中,你不需要

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

    dispatch(fetchOneCategory(slug)) 没有返回承诺,没有 then 被读取

  • 0

    对不起,我会回答我自己的问题:

    我改变了这个:

    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> );
      }
    }
    

    谢谢你的帮助!

相关问题