Guest User

Untitled

a guest
Oct 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class DisplayMessages extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. input: '',
  6. messages: []
  7. }
  8. this.handleChange = this.handleChange.bind(this);
  9. this.handleSubmit = this.handleSubmit.bind(this);
  10. };
  11.  
  12. handleChange(e) {
  13. this.setState({
  14. input: e.target.value
  15. })
  16. }
  17.  
  18. handleSubmit(e) {
  19. e.preventDefault();
  20. this.setState({
  21. messages: this.state.messages.concat(this.state.input),
  22. input: ''
  23. });
  24. }
  25.  
  26. render() {
  27. const listMessages = this.state.messages.map((message, index) =>
  28. <li key={index}>{message}</li>
  29. );
  30. return (
  31. <div>
  32. <h2>Type in a new Message:</h2>
  33. { /* render an input, button, and ul here */ }
  34. <input type="text" onChange={this.handleChange} value={this.state.input} />
  35. <button type="submit" onClick={this.handleSubmit} value="submit">Click</button>
  36.  
  37. <ul>{listMessages}</ul>
  38. </div>
  39. );
  40. }
  41. };
Add Comment
Please, Sign In to add comment