leonardo-m
10/7/2019 - 9:27 PM

dynamic add li to UL React array state

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(e){
    this.setState({input:e.target.value})
  }

  submitMessage(){
    const curMsg = this.state.input;
    this.setState({
      input:"",
      messages:this.state.messages.concat(curMsg)
    });
  }

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

        { /* change code above this line */ }
      </div>
    );
  }
};