Created
September 23, 2019 13:03
-
-
Save fasidOnGit/b0c5be1ee0a1b456535d6f9c9ab767a1 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
class MyComponent extends React.Component { | |
constructor(props) { | |
// set the default internal state | |
this.state = { | |
clicks: 0 | |
}; | |
} | |
componentDidMount() { | |
this.refs.myComponentDiv.addEventListener('click', this.clickHandler); | |
} | |
componentWillUnmount() { | |
this.refs.myComponentDiv.removeEventListener('click', this.clickHandler); | |
} | |
clickHandler() { | |
this.setState({ | |
clicks: this.clicks + 1 | |
}); | |
} | |
render() { | |
let children = this.props.children; | |
return ( | |
<div className="my-component" ref="myComponentDiv"> | |
<h2>My Component ({this.state.clicks} clicks})</h2> | |
<h3>{this.props.headerText}</h3> | |
{children} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment