I am building a React app and I have a code following this logic:
class ComponentTest extends Component {
state = {test: 0}
testingHandler = () => {
console.log(this.state.test)
}
updateHandler = () => {
let test = Math.random()
this.setState({test})
this.testing()
}
render () {
return (
<button onClick={this.updateHandler}>Update</button>
)
}
}
What I need to do is get the updated value of test
in testingHandler()
, however it doesn't happen, testingHandler
just get the past value, it is like the state that is being received by testingHandler
is always one step late.
For example, let's say that test = 0
, then I click on the button that calls updateHandler()
and now test = 1
, after that updateHandler()
calls testingHandler()
, which I expect to get the new value of test
which now is 1
, however I get the old value 0
. If I call updateHandler()
again and update test
to 2
I'll get 1
in testingHandler()
, which is the past value.
How can I get this value updated from testingHandler
?
I did everything that I could imagine, but I couldn't get this done. I believe it has something to do with the render logic of React
, but I don't know exactly how this is working.
P.S. Passing the value as an argument is not an option.