Dispatch request for all elements of an array in react

Viewed 166

I have an array of payloads. Now, I want to dispatch requests for all the elements of the array when a button is clicked. I am trying to map over the array and send all requests but, somehow that only sends request for the last element.

My Array:

arr = [first_payload, second_payload, third_payload]

My Button:

    const dispatch = useDispatch();
    <Button
        text="Generate"
        variant="primary"
        onClick={()=>{
            arr.map(item =>
                {
                    dispatch(
                        fetchConfigAsync.request({
                            item
                        })
                    )
                }
            )
        }} 
    />

My reducer class:

case fetchConfigAsync.successType:
     return {
         ...state,
         state: (state.Config.push(action.response))
    }

However, the above code called the request for only the last element in the array.

0 Answers
Related