Created
October 18, 2018 12:15
-
-
Save jepras/60f8acccf30f3ae50e9364bdea8fd525 to your computer and use it in GitHub Desktop.
Basic setup for inputting a value and rendering it as a list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisplayMessages extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
input: '', | |
messages: [] | |
} | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
}; | |
handleChange(e) { | |
this.setState({ | |
input: e.target.value | |
}) | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
this.setState({ | |
messages: this.state.messages.concat(this.state.input), | |
input: '' | |
}); | |
} | |
render() { | |
const listMessages = this.state.messages.map((message, index) => | |
<li key={index}>{message}</li> | |
); | |
return ( | |
<div> | |
<h2>Type in a new Message:</h2> | |
{ /* render an input, button, and ul here */ } | |
<input type="text" onChange={this.handleChange} value={this.state.input} /> | |
<button type="submit" onClick={this.handleSubmit} value="submit">Click</button> | |
<ul>{listMessages}</ul> | |
</div> | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment