Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 18, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/60f8acccf30f3ae50e9364bdea8fd525 to your computer and use it in GitHub Desktop.
Save jepras/60f8acccf30f3ae50e9364bdea8fd525 to your computer and use it in GitHub Desktop.
Basic setup for inputting a value and rendering it as a list.
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