class DisplayMessages extends React.Component { constructor(props) { super(props); this.state = { input: '', messages: [] } } // add handleChange() and submitMessage() methods here handleChange(event){ this.setState({ input: event.target.value, messages: this.state.messages }) } submitMessage(){ this.setState({ input: '', messages: [...this.state.messages, this.state.input] }) } render() { return ( <div> <h2>Type in a new Message:</h2> { /* render an input, button, and ul here */ } <input onChange={this.handleChange.bind(this)} value={this.state.input}/> <button onClick={this.submitMessage.bind(this)}>Submit</button> <ul> {this.state.messages.map((x, i)=>{ return <li key={i}>{x}</li> })} </ul> { /* change code above this line */ } </div> ); } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter