Manage State Locally First - React and Redux

Tell us what’s happening:

I can’t figure it out my code is not achieving these two things :

-The submitMessage method should clear the current input.
-Clicking the Add message button should call the method submitMessage which should add the current input to the messages array in state.

I keep thinking about it and trying to fix but it looks fine to me :confused: . If someone could help me I would really appreciate.

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);
  }

  handleChange (e) {
    this.setState({input:e.target.value});
  }

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

  render() {
    return (
      <div>
        <h2>Type in a new Message:</h2>
        <input value={this.state.input} onChange={this.handleChange}/>
        <button onClick={this.state.submitMessage}>Add message</button>
        <ul> 
        </ul>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

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

You are not rendering the messages inside the ul.

You need to call the function submitMessage in button OnClick. You are calling submitMessage as a state rather than function and moreover, you need to iterate and return the messages in

  • tag.