Manage State Locally First - Console State Issue

Why can’t I console.log the state.input in submitMessage()?
This is just straight up React, so can’t I just reference the property without using getState()?

Your code so far

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

  }

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/manage-state-locally-first

There is no built-in function named getState that I am aware of.

You can reference the state’s input property by using this.state.input

1 Like

getState is a redux thing, that is a method on the redux store that retrieves the current state (something you rarely need, since reducers also get the current state). As @RandellDawson mentioned, there’s no such method on react components – just grab the state directly from this.state.