State

why does the alert does not get the latest value while the 2nd setter function does?
import { useState } from 'react';

export default function Counter() {
const [number, setNumber] = useState(0);

return (
<>
<h1>{number}</h1>
<button onClick={() => {
setNumber(number => number + 5);
setNumber(number => number + 5);
alert(number);
}}>+5</button>
</>
)
}
import { useState } from 'react';

export default function Counter() {
const [number, setNumber] = useState(0);

return (
<>
<h1>{number}</h1>
<button onClick={() => {
setNumber(number => number + 5);
setNumber(number => number + 5);
alert(number);
}}>+5</button>
</>
)
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server