I have a button:
<button onClick = {() => this.test()}>test</button>
and test() function:
test = () => {
var id2 = uuidv4();
this.setState({
id : id2,
});
console.log("id2 = " + id2);
console.log("state.id = " + this.state.id);
}
The result I get when I click 3 times:
id2 = 122ac169-9cde-42a0-8eb4-8707eec8e1b3
state.id = undefined
id2 = 5a7f36fd-801e-4d41-80db-677c57861fdc
state.id = 122ac169-9cde-42a0-8eb4-8707eec8e1b3
id2 = 651806cc-3893-430d-8356-c73c964213b6
state.id = 5a7f36fd-801e-4d41-80db-677c57861fdc
id2 = 61e3c613-f1c7-4dfb-a1e0-5d465f4297d5
state.id = 651806cc-3893-430d-8356-c73c964213b6
why state.id = undefined although I've set state.id = id2 already?, furthermore,at next times, state.id's values get value of previous value instead get value of id2?