I am trying to select all the text in a textarea when a component loads using React v16.3.1
Following the Refs docs I have a basic sample but this.textarea
is always undefined, if I change this sample to execute the same code on a button click it works fine.
So whats going on? I had expected that after mounting the component should be available?
Sample code:
import React from "react";
class Hello extends React.Component {
constructor(props) {
super(props);
this.textarea = React.createRef();
}
componentDidMount = () => {
this.textarea.current.select();
};
render() {
return (
<div>
<textarea
className="form-control"
defaultValue="the quick brown fox."
ref={this.textarea}
/>
</div>
);
}
}
From package.json:
"react": "^16.3.1",
"react-dom": "^16.3.1",
Thanks