DEV Community

Нурбек Нуржанов
Нурбек Нуржанов

Posted on

React does task queue very weird.

React does task queue very weird.

useEffect(()=>{
if(count===2){
const history2 = history.push('/page2');
console.log('history2', history2)
}
})

const onClickEvent = async ()=>{
await new Promise(resolve => {
setCount(2);
resolve(true);
});
const history1 = history.push('/page1');
console.log('history1', history1);
};
First promise works
Then useEffect works, reacting to state changed
Last command works after promise, it does redirect to page1
This goes out to console

history2 undefined
history1 undefined
I agree with this, first works history2, the last works history1. But, browser does redirect to page2.

Why? Last redirect should be page1.

Why not? Can anyone explain this to me?

Top comments (0)