I am trying to use a ref to get to the underlying input element of an InputText component. I used this.textFieldRef = React.createRef()
to set up the ref, and then the attribute ref={this.textFieldRef}
to hook it up. Yet in the componentDidMount
I cannot use this.textFieldRef.current.select()
because select()
is not a function available for that object. So somehow, InputText is not returning the underlying HTMLInputElement.
Does anyone know how I can get from a ref to something that allows me to select()
the text in the InputText element?
Here is my code, which is actually in TypeScript...
import * as React from 'react';
import { InputText } from 'primereact/inputtext';
export class ValueCard extends React.Component<{}, {}> {
textFieldRef: React.RefObject<any> = React.createRef();
componentDidMount = () => {
if (this.textFieldRef.current instanceof InputText) {
this.textFieldRef.current.select();
}
}
render() {
return = (
<InputText
value="test"
ref={this.textFieldRef}
/>
);
}
}