Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bind in constructor - react
class App extends React.Component {
constructor(props) {
super(props);
this.state = { message: "Hello World!" };
this.logMessage = this.logMessage.bind(this);
}
logMessage = () => {
console.log(this.state.message);
}
render() {
return (
<div className="App">
<input
type="button"
value="Show message"
onClick={this.logMessage}
/>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.