Skip to content

Instantly share code, notes, and snippets.

@vdsabev
Last active December 22, 2018 20:10
Show Gist options
  • Select an option

  • Save vdsabev/afc12cb3e672df9fbf9be3042836f6d0 to your computer and use it in GitHub Desktop.

Select an option

Save vdsabev/afc12cb3e672df9fbf9be3042836f6d0 to your computer and use it in GitHub Desktop.
A Simple Rule for Naming Event Handlers
class EditForm extends React.Component {
state = { discardModalIsShown: false };
// Previously `onClick`
showDiscardModal = () => this.setState({ discardModalIsShown: true });
// Previously `onNo` / `onClose`
hideDiscardModal = () => this.setState({ discardModalIsShown: false });
// Previously `onSubmit`
save = ($event) => {
$event.preventDefault();
this.hideDiscardModal();
this.props.onSave();
};
// Previously `onYes`
discard = () => {
this.hideDiscardModal();
this.props.onDiscard();
};
render() {
return (
<form onSubmit={this.save}>
{/* Some form fields */}
{this.props.children}
<button type="button" onClick={this.showDiscardModal}>Discard</button>
<button type="submit">Save</button>
{this.state.discardModalIsShown && (
<ConfirmationModal
onYes={this.discard}
onNo={this.hideDiscardModal}
onClose={this.hideDiscardModal}
>
Discard all your hard work?
</ConfirmationModal>
)}
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment