React and Redux - Manage State Locally First

Tell us what’s happening:

My code for this challenge seems to work as far as I can tell according to the description, but I am failing most of the tests.

Your code so far

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() {
    const inputBox = document.getElementById("inputBox");
    this.setState((prevState) => ({
      input: inputBox.value,
      messages: prevState.messages
    }));
  }

  submitMessage() {
    this.setState(prevState => ({
      input: '',
      messages: [...prevState.messages.concat(prevState.input)]
    }));
  }

  render() {
    return (
      <div>
        <div>{this.state.input}</div>
        <h2>Type in a new Message:</h2>
        { /* Render an input, button, and ul below this line */ }
        <input id="inputBox" onChange={this.handleChange}/>
        <button onClick={this.submitMessage}>Click Me</button>
        <ul>
          
          {
            this.state.messages.map((elem, index) => <li key={index}>{elem}</li>)
          }
          
        </ul>
        { /* Change code above this line */ }
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0

Challenge Information:

React and Redux - Manage State Locally First

Hi @Josh641

You need to use what was taught in the React course to capture user input.

Happy coding

1 Like

Also, the input needs to be a controlled element (i.e. value should be the state value)

And you are rendering an extra div element that wasn’t asked for.

1 Like

Thank you! This along with taking a few days away seemed to help me.

1 Like

I am not sure if the extra div was the test that the hints recommended me, but I can definitely remove it since I have it working!

1 Like