Couldn't find my mistake

Hello, for https://www.freecodecamp.org/learn/front-end-libraries/react-and-redux/manage-state-locally-first exercise I put this code and it works very well. However, the test says that I do fail from the last two criteria.

I just wanted to know whether I made a mistake or it is just the system (you know, we all get bugs sometimes) does not recognize the solution.

I checked the answer from “Get a Hint” section and copy-pasted that one already but as I said, can you please tell me if I made mistake?

Thank you very much for your help and precious time by the way…

  **My code that is working**

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

// Add handleChange() and submitMessage() methods here
handleChange(event){
  this.setState({
    input: event.target.value,
    messages: this.state.messages
  })
}

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

render() {
  return (
    <div>
      <h2>Type in a new Message:</h2>
      { /* Render an input, button, and ul below this line */ }
      <input onChange={this.handleChange} value={this.state.input}></input> <br />
      <button onClick={this.submitMessage}>Add message</button>
      <ul>{this.state.messages.map(x=> <li>{x}</li>)}</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/88.0.4324.190 Safari/537.36.

Challenge: Manage State Locally First

Link to the challenge:

Hello there,

First issue is here (I am surprised there is no error in the console):

submitMessage() {
  event.preventDefault()

Last issue:

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

Here are the instructions:

The submitMessage() method should concatenate the current message (stored in input ) to the messages array in local state,

Hint:

The order of the operations matter

Hope this helps

1 Like