I've solved this exercise a few times now, but why does my console.log(this.state) return the previous state?(and not the current one)

I just finished working with this exercise, but I’m in the midst of playing around with it, and I came across a question. With the following code, when I console.log(this.state) in my submitMessage() function, it console.logs the previous state? Why is the function operating in that way?

class DisplayMessages extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '',
      messages: []
    }
  }
  // add handleChange() and submitMessage() methods here
    // this.handleChange = this.handleChange.bind(this)
    // this.submitMessage = this.submitMessage.bind(this)

    handleChange(event) {
        this.setState({
          input: event.target.value,
          messages: this.state.messages
        })
    }

    submitMessage() {
      this.setState({
        input: '',
        messages: [...this.state.messages, this.state.input]
        });
        console.log(this.state)
    }

  render() {
    return (
      <div>
        <h2>Type in a new Message:</h2>
        { /* render an input, button, and ul here */ }
        <input onChange={this.handleChange.bind(this)} value={this.state.input}/>
        <button onClick={this.submitMessage.bind(this)}>Submit</button>
        <ul>
        {this.state.messages.map((x,i) =>{return <li key ={i}>{x}</li>})}
        </ul>
        
        { /* change code above this line */ }
      </div>
    );
  }
};

Because it executes asynchronously. Console.log in a callback for the updated state to be reflected.

 this.setState({
        input: '',
        messages: [...this.state.messages, this.state.input]
        } , () => { console.log(this.state)});

Oh, I had no idea that was asynchronous . Good thing I asked, that was important for me to know. Thanks!

You can read more Here and This stackoverflow Question. Both are interesting reads.