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
    })
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be8ebc-54596.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be8ebc-54596.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?