【发布时间】:2020-10-28 12:58:37
【问题描述】:
我使用 react-bootstrap-typeahead 打字稿,从@types/react-bootstrap-typeahead添加类型
在我的 react 功能组件的 typeahead 中,我试图访问 typeahead ref 并调用组件的公共方法,如给定的here
从 'react-bootstrap-typeahead' 导入 { Typeahead };
const typeahead = React.createRef();
<Typeahead
ref={(ref) => typeahead = ref}
id="basic-typeahead-single"
filterBy={['dialCode', 'name']}
inputProps={{
className: 'attrib',
}}
labelKey={(option: any) => `${option.name}`}
onChange={(selected: any) => {
console.log(selected);
}}
renderMenuItemChildren={(option) => (
<div>
<div>{option.name} {option.section}</div>
</div>
)}
options={employeeList}
selected={state.internationalization}>
</Typeahead>
<span className="arrow" onClick={() => {
const instance = typeahead.getInstance();
console.log("instance", instance);
instance.clear();
instance.focus();
}}><img src="/arrow.svg" /></span>
它引发错误 - 类型“RefObject”上不存在属性“getInstance”
所以在创建参考时我尝试过:const typeahead = React.createRef<Typeahead>();
但是打字稿似乎缺少一些东西
【问题讨论】:
标签: reactjs typescript react-bootstrap-typeahead