How redux updates store twice in a row

update the store data twice in a row, and the second time you will get the original store
such as

.
initState ={
    a:"",
    b:""
};
dispatch(setState(a,1))
dispatch(setState(b,1))

the final state result is

{
    a:"",
    b:1
}

if you replace it with


setTimeout(()=>{
    dispatch(setState(b,1))
},0)

can get the correct value

but don"t want a timer in the code

reducer is as follows

const initState = {
    a: "",
    b: ""
}

function test (state = initState, action) {
    switch (action.type) {
    case "TEST_SET":
        return Object.assign({}, state, { [action.key]: action.value });
    default:
        return state;
    }
}

is there a solution

Feb.28,2021

should be default:
return initState, in your reducer. The correct one is
return state


. SetState is asynchronous in

react. However, setState provides a callback function:
you can use it like this:

setState({
    a:1
},function(){
    console.log('a');
    //b
    setState({
        b:1
    })
})
Menu