Created
May 7, 2018 07:02
-
-
Save chnbohwr/2b41dd85f7bc43dc311b4cb62c42c5ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React, { Component } from 'react'; | |
| class MsgItem extends Component { | |
| render() { | |
| return <li style={{ margin: "10px" }}> {this.props.msg} </li>; | |
| } | |
| } | |
| export default class App extends Component { | |
| state = { messages: [] } | |
| addMessage = () => { | |
| const messages = [...this.state.messages]; | |
| messages.push(Math.random()); | |
| this.setState({ messages }); | |
| } | |
| render() { | |
| return ( | |
| <div className="App"> | |
| <button onClick={this.addMessage}>add message</button> | |
| <ul> | |
| { | |
| this.state.messages.map((m, i) => <MsgItem key={i} msg={m} />) | |
| } | |
| </ul> | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment