Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created June 6, 2018 21:28
Show Gist options
  • Save gladchinda/abfa40f1f34be4ce84c35c134173bad9 to your computer and use it in GitHub Desktop.
Save gladchinda/abfa40f1f34be4ce84c35c134173bad9 to your computer and use it in GitHub Desktop.
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