Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Events public api in a react component
import SampleComponent from './sample-component';
export default class App extends React.Component {
constructor(props) {
super(props);
this.editClickHandler = this.editClickHandler.bind(this);
}
editClickHandler() {
//TODO
}
render() {
return <SampleComponent onEdit={this.editClickHandler} />
}
}
export default class SampleComponent extends React.Component {
constructor(props) {
super(props);
this.editClickHandler = this.editClickHandler.bind(this);
}
// Notice Handler postfix here
editClickHandler() {
// TODO
}
render() {
return <div>
...
<button onClick={this.editClickHandler}>Edit</button>
</div>;
}
}
SampleComponent.propTypes = {
title: PropTypes.string.isRequired,
// Notice 'on' prefix
onEdit: PropTypes.func
};
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.