Here's what I currently have.
class App extends React.Component {
constructor(props) {
super(props)
this.textInput = React.createRef();
this.state = {
value: 'select all on click'
}
}
handleClick = e => {
e.preventDefault();
this.setState({ value: this.textInput.current.select()})
};
render() {
return (
<div>
<input type="text" ref={this.textInput}
value={this.state.value}
onClick={this.handleClick}/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
This works, but in my browser I keep getting this error in handleClick:
Cannot read property 'current' of undefined.
This is because I'm using some <Child>
component in place of <input>
in my local environment. I've tried renaming the ref to match the parent prop as suggested here https://stackoverflow.com/a/53241934/6312629, but that also didn't work.
Any suggestions as to how to resolve this would be greatly appreciated!
Thank you very much!