Last active
December 21, 2018 20:16
-
-
Save vdsabev/9b4419478b5864eba866850023ffcce6 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 }; | |
| onSubmit = ($event) => { | |
| $event.preventDefault(); | |
| this.setState({ discardModalIsShown: false }); | |
| this.props.onSave(); | |
| }; | |
| onClick = () => this.setState({ discardModalIsShown: true }); | |
| onYes = () => { | |
| this.setState({ discardModalIsShown: false }); | |
| this.props.onDiscard(); | |
| }; | |
| onNo = () => this.setState({ discardModalIsShown: false }); | |
| onClose = () => this.setState({ discardModalIsShown: false }); | |
| render() { | |
| return ( | |
| <form onSubmit={this.onSubmit}> | |
| {/* Some form fields */} | |
| {this.props.children} | |
| <button type="button" onClick={this.onClick}>Discard</button> | |
| <button type="submit">Save</button> | |
| {this.state.discardModalIsShown && ( | |
| <ConfirmationModal | |
| onYes={this.onYes} | |
| onNo={this.onNo} | |
| onClose={this.onClose} | |
| > | |
| 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