简体   繁体   English

FlatList 在 React Native 中返回空白屏幕

[英]FlatList returning blank screen in react native

I have been trying to render data for view using我一直在尝试使用

FlatList平面列表

but instead of displaying the hard-coded values, it is displaying blank screen.但不是显示硬编码值,而是显示空白屏幕。

This is what I have done do far.这就是我所做的远。

NB: I intentionally omit注意:我故意省略

import elements from react从反应导入元素

import { List, ListItem } from "react-native-elements";

const attendants = [{
       courseName: 'comp',
       lecturer: 'Akanbi T.B',
       students: 'Tunde Ajagba',
      date: '10/11/2020',
      no: 1,
     },{
       courseName: 'comp',
       lecturer: 'Akanbi T.B',
       students: 'Tunde Ajagba',
      date: '09/11/2020',
      no: 2,
     },
     {
       courseName: 'comp',
       lecturer: 'Akanbi T.B',
       students: 'Tunde Ajagba',
      date: '08/11/2020',
      no:3,
     },
     ];

export default class ViewAttendance extends Component {
  constructor(props) {
        super(props);

        this.state = {
            data: [],
        }
    }
    
    componentDidMount() {
      this.setState({
          data: [...this.state.data, ...attendants],
        });
    }

  render() {
    return (
      <Container style={styles.containerStyle}>
        <List>
      <FlatList
        data={this.state.data}
        renderItem={({ item }) => (
          <ListItem
            title={`${item.lecturer} ${item.courseName}`}
            subtitle={item.student}
            keyExtractor={item => item.no}
          />
        )}
      />
    </List>
</Container>
    );
  }
}

What is wrong with the above codes?上面的代码有什么问题? What can I do to make the code work?我该怎么做才能使代码正常工作?

As pointed out by Noor, the most likely problem is with <List> component,正如 Noor 所指出的,最可能的问题是<List>组件,
Though I don't know the actual makeup of ListItem , I tried to emulate it.虽然我不知道ListItem的实际构成,但我试图模仿它。

Here is the working example:这是工作示例:


//import { List, ListItem } from 'react-native-elements';
import React, { Component } from 'react';
import {
  Text,
  View,
  FlatList,
  StyleSheet,
} from 'react-native';

const attendants = [
  {
    courseName: 'comp',
    lecturer: 'Akanbi T.B',
    students: 'Tunde Ajagba',
    date: '10/11/2020',
    no: 1,
  },
  {
    courseName: 'comp',
    lecturer: 'Akanbi T.B',
    students: 'Tunde Ajagba',
    date: '09/11/2020',
    no: 2,
  },
  {
    courseName: 'comp',
    lecturer: 'Akanbi T.B',
    students: 'Tunde Ajagba',
    date: '08/11/2020',
    no: 3,
  },
];

export default class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [],
    };
  }

  componentDidMount() {
    this.setState({
      data: [...this.state.data, ...attendants],
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          data={this.state.data}
          renderItem={({ item }) => (
            <ListItem
              title={`${item.lecturer} ${item.courseName}`}
              subtitle={item.students}
              keyExtractor={(item) => item.no}
            />
          )}
        />
      </View>
    );
  }
}

const ListItem = ({title, subtitle}) => {
  return (
    <>
      <View style={styles.listContainer}>
        <Text style={styles.title}>
          {title}
        </Text>
        <Text style={styles.subtitle}>{subtitle}</Text>
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  container: {
    marginTop: 20,
  },
  listContainer: {
    height: 40,
    backgroundColor: 'teal',
    margin: 5,
    paddingHorizontal: 5,
    borderRadius: 4,
  },
  subtitle: { fontSize: 12, color: 'rgba(0,0,10,0.5)' },
  title: { fontSize: 14, color: 'white', fontWeight: 'bold' },
});


Screenshot:截屏:

在此处输入图片说明

Live Expo Snack Link现场世博小吃链接

Change item.student to item.students更改item.studentitem.students

subtitle={item.student}

and remove <List> from code并从代码中删除<List>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native FlatList显示-顶部空白 - react-native FlatList displaying - the blank on the top React Native Flatlist 搜索从 firebase 用户集合中返回没有值 - React Native Flatlist search returning no values from firebase users collection 在Flatlist项目中反应Native ref。 仅返回最后一个项目 - React Native ref in Flatlist items. Returning for last item alone react-native,客户平面列表选择器,在选择时返回以前的值 - react-native , customer flatlist picker, returning previous values on selection 在React Native的当前屏幕上显示时,如何主动刷新FlatList? - How to actively refresh a FlatList whenever it shows on the current screen in React Native? React Native:将嵌套数组传递到另一个屏幕并在平面列表中输出 - React Native: Passing nested array to another screen and output in flatlist 如何在 React Native 中使用可滚动的 FlatList 制作屏幕? - How can you make a screen with a FlatList scrollable in React Native? React Native:我可以将项目添加到另一个屏幕中的平面列表中吗? - React Native: Can i add items into flatlist that is in another screen, React-Native FlatList 白屏不加载数据 - React-Native FlatList White Screen doesn't load datas 如何在反应原生中单击平面列表中的项目来打开屏幕? - How to open screen wiith click on item in flatlist in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM