我正在尝试使用 ref 来访问 InputText 组件的底层输入元素。我曾经this.textFieldRef = React.createRef()
设置 ref,然后设置属性ref={this.textFieldRef}
来连接它。然而,在componentDidMount
I cannot use this.textFieldRef.current.select()
because select()
is not a function available for that object 中。所以不知何故,InputText 没有返回底层的 HTMLInputElement。
有谁知道我如何从 ref 获取允许我访问select()
InputText 元素中的文本的内容?
这是我的代码,实际上是在 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}
/>
);
}
}