Last active
December 22, 2018 20:10
-
-
Save vdsabev/afc12cb3e672df9fbf9be3042836f6d0 to your computer and use it in GitHub Desktop.
A Simple Rule for Naming Event Handlers
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 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