Created
January 30, 2018 22:21
-
-
Save chris-burgin/1f165997ad7b45c7d863c50d238a3cec to your computer and use it in GitHub Desktop.
An example of PureComponent in react
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, PureComponent } from "react" | |
export default class App extends Component { | |
constructor() { | |
super() | |
this.state = { messages: [] } | |
// binds | |
this._getLastMessage = this._getLastMessage.bind(this) | |
this._onMessageChange = this._onMessageChange.bind(this) | |
} | |
_getLastMessage() { | |
const lastMessage = this.state.messages[this.state.messages.length - 1] | |
return lastMessage === undefined ? "" : lastMessage | |
} | |
_onMessageChange(event) { | |
const messages = [...this.state.messages] | |
messages.push(event.target.value) | |
this.setState({ messages }) | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<input | |
type="text" | |
value={this._getLastMessage()} | |
onChange={this._onMessageChange} | |
style={{ margin: "10px" }} | |
/> | |
<MessageList messages={this.state.messages} /> | |
</div> | |
) | |
} | |
} | |
class MessageList extends Component { | |
render() { | |
return ( | |
<ul> | |
{this.props.messages.map((m, i) => <Message key={i} message={m} />)} | |
</ul> | |
) | |
} | |
} | |
class Message extends PureComponent { | |
render() { | |
return <li style={{ margin: "10px" }}> {this.props.message} </li> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment