Reactjs 使用Typescript进行异步typeahead反应
我正在尝试使用React using tsx文件中的模块React引导类型预发(和版本) Im使用以下版本:Reactjs 使用Typescript进行异步typeahead反应,reactjs,react-bootstrap-typeahead,Reactjs,React Bootstrap Typeahead,我正在尝试使用React using tsx文件中的模块React引导类型预发(和版本) Im使用以下版本: "@types/react-bootstrap-typeahead": "3.4.5" "react-bootstrap-typeahead": "3.4.3" 我构建了下面的SimpleAsyncExample类,试图通过始终返回相同的结果来测试组件,无论用户类型如何,但响应延迟 import*as React from'React'; 从“react bootstrap type
"@types/react-bootstrap-typeahead": "3.4.5"
"react-bootstrap-typeahead": "3.4.3"
我构建了下面的SimpleAsyncExample类,试图通过始终返回相同的结果来测试组件,无论用户类型如何,但响应延迟
import*as React from'React';
从“react bootstrap typeahead”导入{AsyncTypeahead,ClearButton,Token};
界面状态{
大写:字符串;
名称:字符串;
人口:个;
区域:字符串;
setValue:(值:状态)=>无效;
}
常量选项:状态[]=[
{姓名:'Alabama',人口:4780127,首都:'Montgomery',地区:'South',设定值:()=>{},
{姓名:'阿拉斯加',人口:710249,首都:'朱诺',地区:'西部',设定值:()=>{},
{名称:'Arizona',人口:6392307,首都:'Phoenix',地区:'West',设定值:()=>{},
{姓名:'阿肯色州',人口:2915958,首府:'小石城',地区:'南',设定值:()=>{},
{姓名:'California',人口:37254503,首府:'Sacramento',地区:'West',setValue:()=>{},
{姓名:'Colorado',人口:5029324,首府:'Denver',地区:'West',设定值:()=>{},
];
类型propTypes={};
类型defaultProps={
isLoading:布尔值,
选项:国家[]
};
导出默认类SimpleAsyncExample扩展React.Component{
状态={
孤岛加载:false,
选项:[]
};
render(){
返回(
{
返回console.log(props.text)}>
{selectedItem.name}{}/>
;
}}
/>
);
}
onSearch=(查询:字符串)=>{
this.setState({isLoading:true});
设置超时(()=>{
this.setState({isLoading:false,options,});
}, 2000);
}
}
我还配置了(如文档所示)css导入
import 'react-bootstrap-typeahead/css/Typeahead.css';
import 'react-bootstrap-typeahead/css/Typeahead-bs4.css';
此应用程序不工作,不显示任何结果
问题是,在测试或示例中,没有涉及AsyncTypeahead组件,我也没有找到任何示例,因此,如果有人有一个解决方案,或者可能有另一个模块建议,可以使用Bootstrap 4和React(使用Typescript)或者使用此库的工作示例将非常感谢。您需要指定正确的
labelKey
字段。默认情况下,该值为“label”,但在您的情况下,选项中没有“label”字段,因此过滤器将失败。将labelKey
设置为“name”可解决以下问题:
<AsyncTypeahead
{...this.state}
id="typeahead"
delay={800}
emptyLabel="No se encontraron resultados"
ignoreDiacritics={true}
labelKey="name" // <---------- You're missing this
minLength={3}
onSearch={this.onSearch}
placeholder="Insert text to search"
promptText="Searching"
searchText="Searching"
/>
你的问题是什么?@ericgio该实现不起作用,因此建议使用一个解决方案、另一个模块建议或一个工作示例。添加该行会引发以下错误类型“string”不可分配给类型“undefined”。
@Juliovillan:对我来说可以吗?我上面有相同的错误消息,但情况不同,所以我将在这里添加我的解决方案,以供将来的读者使用。通过使我的选项状态变量强类型化,解决了这个问题。我认为底层类型定义对标签字段可以是的类型有特定的要求,我认为这就是它失败的原因。@ericgio您给出的示例第行有一个错误85@unclenorton你说得对。类型定义似乎有问题<代码>标签键
不应是可选的。