【问题标题】:How to display data using array in flatlist react-native如何在 flatlist react-native 中使用数组显示数据
【发布时间】:2020-01-22 10:48:03
【问题描述】:

如何在 react-native 中使用 flatlist 中的数组显示数据。

我想输出这样的东西。

【问题讨论】:

标签: react-native react-native-android react-native-ios react-native-flatlist


【解决方案1】:

试试这是一个你可以使用自己的虚拟示例:

import React from 'react';
import { SafeAreaView, View, FlatList, StyleSheet, Text } from 'react-native';
import Constants from 'expo-constants';

const DATA = [
  {
    id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
    title: 'First Item',
  },
  {
    id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
    title: 'Second Item',
  },
  {
    id: '58694a0f-3da1-471f-bd96-145571e29d72',
    title: 'Third Item',
  },
];

function Item({ title }) {
  return (
    <View style={styles.item}>
      <Text style={styles.title}>{title}</Text>
      <Text>Button</Text>
    </View>
  );
}

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <FlatList
        data={DATA}
        renderItem={({ item }) => <Item title={item.title} />}
        keyExtractor={item => item.id}
      />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: Constants.statusBarHeight,
  },
  item: {
    backgroundColor: '#f9c2ff',
    padding: 20,
    marginVertical: 8,
    marginHorizontal: 16,
    flexDirection:'row'
  },
  title: {
    fontSize: 32,
  },
});

【讨论】:

    【解决方案2】:

    创建一个所需布局的自定义组件,然后在 flatlist 的 renderItem 属性中传递该布局 JSX。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 2021-10-21
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多