Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created June 6, 2018 21:33
Show Gist options
  • Select an option

  • Save gladchinda/d75c54f03094f25221380f2d568a8626 to your computer and use it in GitHub Desktop.

Select an option

Save gladchinda/d75c54f03094f25221380f2d568a8626 to your computer and use it in GitHub Desktop.
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