Redux Action映射对象内的空数组

时间:2019-07-08 17:01:25

标签: reactjs redux react-redux axios

我从一个动作(使用axios)获取一个对象,并使用map函数对其进行迭代。

我还需要执行其他操作,但要在父对象内部进行映射。 我看到请求/响应没问题(返回的数据),但是reducer变量仍然为空。

1:组件获取数据

componentDidMount() {
        const { match: { params } } = this.props;
        this.props.getSaleDetails(params.order_id);
      }

2:定义mapStateToProps和mapDispatchToProps

const mapStateToPropos = state => ({
    saleDetails: state.salesOrders.saleDetails,
    saleDetailFurthers: state.salesOrders.saleDetailFurthers
   });

const mapDispatchToProps = dispatch =>
  bindActionCreators({ getSaleDetails, getDetailFurthers }, dispatch);

3:从redux道具创建const

const detailsArray = saleDetails.data;

4:使用地图函数迭代数组

// getDetailFurthers is another action, getting data by passing "detail_id" and updating "saleDetailFurthers" props

{detailsArray && detailsArray.map((item) => {
                    const {getDetailFurthers, saleDetailFurthers} = this.props;
                    getDetailFurthers(item.detail_id)
                    console.log(saleDetailFurthers)
// empty array????
                    count++;
                    return (
                        <Paper className={classes.paper} key={item.detail_id}>
// ... next code lines

5:操作

export function getDetailFurthers(detail_id){
    return async dispatch => { 
        const request =  await axios.get(`${BASE_URL}/salesorders/detail/furthers/${detail_id}`)
        return {
            type: "SALE_DETAIL_FURTHERS_FETCHED", 
            payload: request
        }
    }
}

6:减速器

const INITIAL_STATE = {
  //... others
  saleDetailFurthers: []
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    ///... other cases
    case "SALE_DETAIL_FURTHERS_FETCHED":
      return { ...state, saleDetailFurthers: action.payload }
    default:
      return state
  }
};

我希望“ saleDetailFurthers”常量会加载来自redux操作的数据。

2 个答案:

答案 0 :(得分:0)

您需要使用派遣而不是返回,就像这样:

Gson gson = new GsonBuilder()
               .registerTypeAdapter(Date.class, new GsonDateAdapter())
               .create();

答案 1 :(得分:0)

export function getDetailFurthers(detail_id) => dispatch =>{
    const request =  await axios.get(`${BASE_URL}/salesorders/detail/furthers/${detail_id}`)
    dispatch ({
        type: "SALE_DETAIL_FURTHERS_FETCHED", 
        payload: request
    })
}