1

I have the following initial state and slice objects. How do I add values to the hand array every second using dispatch? My intention is to have each dispatch be completed before the next one. I looked at createAsyncThunk but I wasn't clear on how to implement it on the same action repeatedly with a time interval.

const initialState = { 
    hand: [],
};

export const stateSlice = createSlice({
    name: 'black',
    initialState,
    reducers: {
        updateHand: (state, action) => {
            state.hand = [...state.hand, action.payload]
        }
    }
});

Thanks.

Dag
  • 33
  • 4
  • Is this answer your question? [https://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout?rq=1](https://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout?rq=1) – DaniloCadeddu Aug 22 '21 at 07:10
  • @DaniloCadeddu Yes I looked at that and tried the code below but it never executed the second dispatch inside the setTimeout function. `dispatch(updateHand([ ...stateSelector.hand, card1 ])) setTimeout(()=> { updateHand([ ...stateSelector.hand, card2 ]) }, 1000)` I am assuming I need some async redux thunk but not sure how to implement it for my case because the second dispatch depends on the result of the first dispatch. – Dag Aug 22 '21 at 13:32

0 Answers0