Created
October 21, 2018 16:50
-
-
Save valex/fc17ed66bd33476a722a246c71e5215e to your computer and use it in GitHub Desktop.
React.PureComponent
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Lifecycle (with mixin)</title> | |
| <style> | |
| body, textarea { | |
| font-family: Courier; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="app"> | |
| <!-- my app renders here --> | |
| </div> | |
| <script charset="utf-8" crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> | |
| <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
| <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> | |
| <script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.6.2/prop-types.min.js"></script> | |
| <script type="text/babel"> | |
| class Counter extends React.PureComponent { | |
| render() { | |
| console.log('Counter::render()'); | |
| return (<h3>{this.props.count}</h3>); | |
| } | |
| } | |
| Counter.propTypes = { | |
| count: PropTypes.number.isRequired, | |
| }; | |
| class TextAreaCounter extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| text: props.defaultValue | |
| }; | |
| this._textChange = this._textChange.bind(this); | |
| } | |
| static defaultProps = { | |
| defaultValue: 'default text' | |
| }; | |
| _textChange(event) { | |
| this.setState({ | |
| text: event.target.value, | |
| }); | |
| }; | |
| render() { | |
| console.log('TextAreaCounter::render()'); | |
| var counter = null; | |
| if (this.state.text.length > 0) { | |
| counter = React.createElement(Counter, { | |
| count: this.state.text.length, | |
| }); | |
| } | |
| return ( | |
| <div> | |
| <textarea onChange={this._textChange} value={this.state.text}></textarea> | |
| {counter} | |
| </div> | |
| ); | |
| }; | |
| } | |
| TextAreaCounter.propTypes = { | |
| defaultValue: PropTypes.string | |
| }; | |
| var myTextAreaCounter = ReactDOM.render( | |
| React.createElement(TextAreaCounter, { | |
| defaultValue: "Bob", | |
| }), | |
| document.getElementById("app") | |
| ); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment