1

I'm getting an object from an action (using axios) and using a map function to iterate it.

I also need to get another action but inside the parent object mapped. I see that the request/response are ok (with returned data), but the reducer variable still gets empty.

1: component gets data

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

2: defining mapStateToProps and mapDispatchToProps

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

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

3: creating a const from the redux props

const detailsArray = saleDetails.data;

4: iterate array with map function

// 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: Actions

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: Reducers

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

I expect the "saleDetailFurthers" const be loaded with data from redux action.

1
  • tried to use SALE_DETAIL_FURTHERS_FETCHED instead "SALE_DETAIL_FURTHERS_FETCHED" in Reducer and your Action Commented Jul 8, 2019 at 18:12

2 Answers 2

0

You need to use dispatch instead of returning, like so:

dispatch({
  type: "SALE_DETAIL_FURTHERS_FETCHED", 
  payload: request
});
Sign up to request clarification or add additional context in comments.

2 Comments

It worked. But it caused an infinite loop request (axios.get).
So when your component first renders it will map over detailsArray and the callback function will call the action creator getDetailFurthers() to fetch data. Once getDetailFuthers() completes the axios call, it will change the redux state and thus cause a re-render of your component since your component is connected to "saleDetailFurthers". Since your component re-renders it will map over detailsArray again and then call getDetailFurthers() and so an infinite loop will happen.
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
    })
}

1 Comment

Dispatch expects an object. Correct your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.