Skip to content

Instantly share code, notes, and snippets.

@chnbohwr
Created May 7, 2018 07:02
Show Gist options
  • Save chnbohwr/2b41dd85f7bc43dc311b4cb62c42c5ce to your computer and use it in GitHub Desktop.
Save chnbohwr/2b41dd85f7bc43dc311b4cb62c42c5ce to your computer and use it in GitHub Desktop.
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