调度操作在回调中返回undefined

时间:2017-01-27 06:15:49

标签: reactjs

在我的父组件中,我有:

getCatalogItems(id = 12, sessionId = "anythingForNow", filterBy = []) {
    return this.props.actions.getCatalogInfo(id, sessionId, filterBy)
      .then(catalogItemsReturned=> {
        console.log("CATALOG ITEMS RETURED", catalogItemsReturned) //always undefined
      })
 }

  componentWillMount() {
    console.log("component will mount called")
    this.getCatalogItems();
  }
由于某种原因,

render此组件的功能被调用为5-6次,即使我根本没有改变任何状态。

  1. 我将动作的结果作为prop(我正在使用redux)通过map状态传递给props,并且当它自己调用第3次或第4次时,此触发动作的结果在render函数中可用。
  2. 是什么原因?我怎么能解决它?

    我的触发操作是通过API调用获得结果,但出于某种原因,我没有在then功能

    中的getCatalogItems部分调度操作中获取结果

    这就是我的行动:这有什么问题吗?

    export function getCatalogItemsSuccess(catalogItems) {
      return {
        type: types.LOAD_CATALOG_SUCCESS,
        catalogItems   //courses:courses
      }
    }
    
    
    export function getCatalogInfo(id = 12, sessionId="anythingForNow", filterBy = []){ 
          return function (dispatch) {
        return catalogProductsAPI.getProductsByCategoryId(id, sessionId, filterBy)
          .then(
          catalogItems =>{
            console.log("catalog INFO in actinnnnnnnnnnnnnnnnn", catalogItems) // I get the right results here
            dispatch(getCatalogItemsSuccess(catalogItems));
          }).catch(err=>{
            console.log("errorin get catalog", err);
          throw(err);
        }); //the function from API would return a promise
    
      };
    }
    

0 个答案:

没有答案