Created
June 6, 2018 21:33
-
-
Save gladchinda/d75c54f03094f25221380f2d568a8626 to your computer and use it in GitHub Desktop.
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 MyComponent extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.inputField = React.createRef(); | |
| this.toggleInputCase = this.toggleInputCase.bind(this); | |
| this.state = { uppercase: false }; | |
| } | |
| toggleInputCase() { | |
| const isUpper = this.state.uppercase; | |
| // Accessing the ref using this.inputField.current | |
| const value = this.inputField.current.value; | |
| this.inputField.current.value = | |
| isUpper | |
| ? value.toLowerCase() | |
| : value.toUpperCase(); | |
| this.setState({ uppercase: !isUpper }); | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| {/* Referencing the ref from this.inputField */} | |
| <input type="text" ref={this.inputField} /> | |
| <button type="button" onClick={this.toggleInputCase}> | |
| Toggle Case | |
| </button> | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment