react-native - Tab ScrollView 内的 FlatList
问题描述
我正在尝试使用 highScores 的键和值创建一个 FlatList,并将它们放在我的 ScrollView 中,但没有呈现......有什么问题?
import React from 'react';
import {
Platform,
ScrollView,
StyleSheet,
Text,
View,
FlatList,
Row
} from 'react-native';
export default function HomeScreen() {
const highScores = { '1': 'a', '2': 'b' };
return (
<View style={styles.container}>
<ScrollView
style={styles.container}
contentContainerStyle={styles.contentContainer}>
<View>
<FlatList
data={highScores}
renderItem={
({ item, index }) =>
<Row highScore={item} index={index} key={index} />
}
/>
</View>
);
}
如您所见,我创建了一个视图来呈现他的项目。我没有收到任何错误,但它不起作用。有人可以帮助我吗?谢谢!
解决方案
实际上,您的问题缺乏解释。不过,我回答了您面临的相关问题。
- 首先,Flatlist 也是一个可滚动的组件,所以在滚动视图中使用 flat list 是不合逻辑的。如果您正在尝试实现嵌套滚动视图,那么您可以继续它。
- 其次,代码中没有关闭标签。它不完整。
- 最后,您已将 JSON 对象赋予 Flatlist 数据道具,flatlist 无法迭代该对象。所以你应该给一个数组来使数组中的项目被渲染。
给予数据道具的正确方式:
const highScores = [
{ '1': 'a' },
{ '2': 'b' },
{ '3': 'c' }
];
解决您的问题:
import * as React from 'react';
import { Text, View, StyleSheet, ScrollView, FlatList } from 'react-native';
import Constants from 'expo-constants';
const highScores = [{ '1': 'a'}, {'2': 'b' }];
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<ScrollView>
<View style={{ width: '100%', height: '100%' }}>
<FlatList
data={highScores}
renderItem={({ item, index }) => <Text>{JSON.stringify(item)}</Text>}
/>
</View>
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
清零了??如果是,请点赞
推荐阅读
- selenium-webdriver - 为什么 data-* 属性比普通 ID 属性更适合元素选择?
- html - 响应式顶部导航未正确响应
- regex - 删除列中重复的子字符串,只返回中间的单词
- node.js - Firebase 身份验证异步等待
- macos - errors when building electron from source with gn and ninja on Mac OS 10.15
- arrays - 将简单代码集成到复杂代码中
- java - 更改 BroadcastReceiver 中的变量
- reactjs - 在远程服务器上运行开发时,webpack 热重载不起作用
- node.js - 使用 socket.io 向特定客户端发送消息,同时套接字 id 快速变化
- orientdb - 如何在 OrientDB 中遍历并找到所有最长的路径?