Created
June 6, 2018 21:28
-
-
Save gladchinda/abfa40f1f34be4ce84c35c134173bad9 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.toggleInputCase = this.toggleInputCase.bind(this); | |
this.state = { uppercase: false }; | |
} | |
toggleInputCase() { | |
const isUpper = this.state.uppercase; | |
// Accessing the ref using this.inputField | |
const value = this.inputField.value; | |
this.inputField.value = | |
isUpper | |
? value.toLowerCase() | |
: value.toUpperCase(); | |
this.setState({ uppercase: !isUpper }); | |
} | |
render() { | |
return ( | |
<div> | |
{/* Creating a callback ref and storing it in this.inputField */} | |
<input type="text" ref={elem => this.inputField = elem} /> | |
<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