react-native - 在 react-native 中使用 FlatList 时数据未呈现
问题描述
我正在尝试使用 flatlist 呈现在屏幕上输入的数据,以便滚动选项适用于我的应用程序。没有显示错误,但数据未呈现。
这是我的代码:
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button, TextInput, ScrollView, FlatList } from 'react-native';
export default function App() {
const [enteredGoal, setEnteredGoal] = useState('');
const [courseGoals, setCourseGoals] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
};
const addGoalHandler = () => {
setCourseGoals(currentGoals => [...courseGoals, { id: Math.random().toString(), value: enteredGoal }]);
};
return (
<View style={styles.screen}>
<View style={styles.inputContainer}>
<TextInput placeholder="Course Goal"
style={styles.input}
onChangeText={goalInputHandler}
value={enteredGoal} />
<Button title="Add" onPress={addGoalHandler} />
</View>
<FlatList
keyExtractor={(item, index) => item.id}
data={courseGoals} renderItem={itemData => {
courseGoals.map((goal) =>
<View style={styles.listItem}><Text>{itemData.item.value}</Text></View>)
}} />
</View>
);
}
const styles = StyleSheet.create({
screen: {
padding: 50
},
inputContainer: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center' },
input: {
width: '80%', borderColor: 'black', borderWidth: 1, padding: 10
}
, listItem: {
padding: 10,
margin: 10,
backgroundColor: '#ccc',
borderColor: 'black',
borderWidth: 1
}
});
你能帮帮我吗?
解决方案
我认为该项目没有正确返回,renderItem
并且您不需要迭代courseGoals
它,因为它已经绑定到data
.
尝试将您的更新renderItem
为:
renderItem={itemData => (
<View style={styles.listItem}><Text>{itemData.item.value}</Text></View>
)}
推荐阅读
- r - 编织 RMarkdown 文档时出现 YAML 错误 - 扫描仪错误
- linq - 如何在 linq 中与运营商一起使用英国格式的日期
- zend-framework - 如何为 SocialEngine 模块设置默认操作
- c# - 将 2 层架构转换为 3 层架构
- java - org.javalite.activejdbc.DBException:在环境属性文件中找不到配置:开发
- julia - Julia 中缺失值的类型,例如 NA_real_
- r - 应用于少数数据点时 gsub 出错
- android - 附加 PDF - Java.Lang.SecurityException:权限被拒绝
- php - Apache/Laravel 错误:分段错误 (11),/etc/apache2 中可能的核心转储
- php - 在 symfony 控制器中找不到文件