Created
June 6, 2018 21:20
-
-
Save gladchinda/82d6d46caf91f6b805288fafaceb052b 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.refs.inputField | |
const value = this.refs.inputField.value; | |
this.refs.inputField.value = | |
isUpper | |
? value.toLowerCase() | |
: value.toUpperCase(); | |
this.setState({ uppercase: !isUpper }); | |
} | |
render() { | |
return ( | |
<div> | |
{/* Creating a string ref named: inputField */} | |
<input type="text" ref="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