In React i have a function called addOrder that adds object with parameter values to state called data. When i try to console.log(this.state.data) i get an empty array for some reason. When i click button that calls addOrder function again i get array that contains objects with parameter values. Why doesn't console.log show me array values the first time i click button?
import React, { Component } from 'react';
class App extends Component {
constructor(){
super()
this.state = {
data:[]
}
}
addOrder(parameter1, parameter2, parameter3){
this.setState({
data:this.state.data.concat(
{
one:parameter1,
two:parameter2,
three:parameter3
}
)
});
console.log(this.state.data);
}
render() {
return (
<div>
<button onClick={() => this.addOrder('1','2','3')}>ok</button>
</div>
)
}
}
export default App;
I expected console.log to show [{one: "1", two: "2", three: "3"}], but instead i got empty array []