react-native - 我无法在反应原生平面列表中写入输入
问题描述
我正在尝试学习 React Native。我有一个示例,但 FlatList 中存在问题。我在页脚组件中有一个 TextInput。没有错误,但是当我尝试写入输入字段时,键盘无法保留。它只是出现然后消失。我该如何解决这个问题?这是我的代码:
import React, { useRef, useState } from "react";
import {
Text,
View,
ActivityIndicator,
Image,
Button,
Alert,
TouchableOpacity,
TextInput,
FlatList,
} from "react-native";
function MyHeader() {
return (
<View style={{ marginHorizontal: 40, marginVertical: 60 }}>
<Text style={{ fontWeight: "bold", fontSize: 24, marginBottom: 30 }}>
Hello React Native
</Text>
<ActivityIndicator
size="large"
color="#c1262c"
style={{ marginBottom: 30 }}
/>
</View>
);
}
function MyFooter() {
const [text, setText] = useState("");
const inputText = (text) => {
setText(text);
};
const alertMessage = text;
return (
<View>
<View
style={{
borderWidth: 2,
borderColor: "black",
padding: 20,
marginBottom: 30,
}}
>
<Text>Hello again!</Text>
</View>
<TextInput
style={{
borderWidth: 2,
borderColor: "black",
padding: 20,
marginBottom: 30,
}}
value={text}
onChangeText={inputText}
/>
<Button
onPress={() => Alert.alert(alertMessage)}
title="Learn More"
color="#c1262"
/>
</View>
);
}
export default function App() {
const randomNum = useRef(Math.random()).current;
return (
<FlatList
ListHeaderComponent={MyHeader}
data={[0, 1, 2, 3, 4]}
renderItem={({ item }) => {
return (
<TouchableOpacity
onPress={() => Alert.alert(`You pressed image #${item + 1}`)}
>
<Image
source={{
uri: `https://picsum.photos/500/300?random=${randomNum + item}`,
}}
style={{ width: "100%", height: 160, marginBottom: 30 }}
/>
</TouchableOpacity>
);
}}
keyExtractor={(item) => String(item)}
ListFooterComponent={MyFooter}
/>
);
}
解决方案
只需更改或删除 onChangeText={inputText}
推荐阅读
- python - 使用属性获取器和设置器进行项目分配?
- excel - 显示定时消息框
- r - 计算点数并在 ggplot 中显示结果(用于混淆矩阵)
- angular - 如何在谷歌标签管理器中使用 MutationObserver 来检测 DOM 中的新元素?
- mysql - 结合独特约束的软删除解决方案?
- java - JdbcTemplate.query 方法未维护列表中逗号分隔字符串的顺序
- python - 哪种模型可以更好地对共线性自变量进行分类?
- spring-boot - FeignClient 编码获取参数(如果作为 URL 的一部分传递)
- timezone - MariaDB convert_tz()(通过即时转换时区复制数据)
- javascript - vendor.js 中的 SCRIPT1002 语法错误 - Angular 8 IE11