State Locally SOLUTION WORKS BUT TESTS FAIL

Tell us what’s happening:
I have written out the solution and everything seems to be working perfectly. however i still can’t get the last test to pass. I have no idea why. everything seems fine

Your code so far


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

  }
  submitMessage(){
    message: this.state.messages.push(this.state.input)
    this.setState({
      input: ''
    })
    console.log(this.state.messages)
    console.log(this.state.input)
  }
   

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36.

Link to the challenge:

This line of code right there:
message: this.state.messages.push(this.state.input)
First of all, you are not in object or return mode here, so I am not sure why you are using “:” instead of =. Secondly, you are manually modifying state here by calling “push” method.
You are not supposed to modify state ever, you do changes to state only by setState method.
Read up this part:
https://learn.freecodecamp.org/front-end-libraries/react/set-state-with-this-setstate/
Hint:
Proper solution will only have this.setState call in submitMessage() and nothing else.

1 Like

Thanks i managed to get it write with some further research on the forum. You’r suggestion is completely write. thanks for the quick reply. I appreciate it.