react-native - 自定义反应本机自动完成输入不起作用
问题描述
我正在尝试构建自定义自动建议组件。我的建议列表没有显示。
Strings must be rendered in a <Text> component
每当我清除该TextInput
字段时,我也会收到错误消息。
我的数据格式如下:
const Data = [{name:'naebi', id:..},{}]
以下是App.js
文件的完整代码。但与我的预期相反,我没有在TextInput
. 我也尝试过 Flatlist,也尝试过更改宽度和高度。
import React, {useState} from 'react';
import {
Text,
View,
StyleSheet,
TextInput,
} from 'react-native';
export default function (props) {
const [filteredSugestions, setFilteredSugestion] = useState([])
const [showSugestions, SetShowSugestions] = useState([])
const [input, setInput] = useState()
const filterSuggestions = (input) => {
Data.filter((data) => {
if(data.name.toLowerCase().includes(input.toLowerCase())){
setFilteredSugestion(data)
SetShowSugestions(true)
}
})
}
const SuggestionsListComponent = () => {
return filteredSugestions.length ? (
filteredSugestions.map((suggestion, index) => {
return (
<Text>{suggestion.name}</Text>
);
})
) : (
<View >
<Text>{input}</Text>
</View>
);
};
return (
<View >
<TextInput
style={{height: 40}}
placeholder="Type here to search"
onChangeText={text => {
setInput(text)
filterSuggestions(text)
}}
/>
{showSugestions && input && <SuggestionsListComponent />}
</View>
);
}
解决方案
推荐阅读
- google-apps-script - 如何使用 Google 表格通过电子邮件发送单行中的指定单元格
- node.js - Passport Facebook-Login 在节点和反应应用程序中因 401 错误而失败
- node.js - 包含 res.locals 的中间件不工作
- python - 比较时,Django 日期字段永远不会相等
- python - 查看页面源码时发现抓取代码
- c# - 如何在动态 Sendgrid 事务模板的模板数据中允许 html?
- php - 将 ajax 调用转换为 fetch api 调用并将信息拉入 php 函数
- c# - 一种获得深层价值的安全方法
- ruby-on-rails - 由于存在资产,搜索机器人无法索引 rails heroku
- android - 未发送请求代码的片段中的 UCrop