Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my FlatList not showing?

I am trying to load this FlatList but it is not showing anything in the screen and I am not getting errors. The breed console.log is logging an array of objects with keys of $t.

import React, { Component } from 'react';
import { FlatList, Text, View } from 'react-native';
import { connect } from 'react-redux';
import { Container, Content } from 'native-base';
// import BreedListItem from './BreedListItem';

class BreedList extends Component {
    render() {
        console.log(this.props.breeds.breed);
        return (
            <Container>
                <Content>
                    <FlatList 
                    style={{ flex: 1 }}
                    dataSource={this.props.breeds.breed}
                    renderItem={(breed) => {
                        console.log(breed.$t);
                        return (
                        <View><Text>{breed.$t}</Text></View>
                    );
                }
                }
                    keyExtractor={(breed) => `${breed.$t}`}
                    />
                </Content>
            </Container>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        breeds: state.breeds.breeds
    };
};

export default connect(mapStateToProps)(BreedList);

The console log of this.props.breeds.breed is below.

(37) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{$t: "Affenpinscher"}
1
:
{$t: "Afghan Hound"}
2
:
{$t: "Airedale Terrier"}
...
like image 948
dbarks Avatar asked Jul 16 '18 00:07

dbarks


People also ask

What can I use instead of FlatList?

Meet FlashList! A FlatList alternative that runs on UI thread and, as they claim on their website, it runs 10x faster on JS and 5x faster on JS thread. Even considering only half the improvement — these are really great performance boosts. pod install if you need to replace your existing <FlatList> with <FlashList>.

How do you show data in FlatList in React Native?

Displaying a List with a React Native FlatList. The FlatList component requires two props: data and renderItem. A data prop takes an array of data that needs to be rendered, and renderItem defines a function that takes data via parameters and returns a formatted component to be displayed on the screen.

How do I refresh FlatList automatically in React Native?

React Native RefreshControl If you're rendering a list of data using the ScrollView or FlatList component, you can use RefreshControl to add the pull-to-refresh capability. When you initiate the pull-to-refresh gesture, RefreshControl triggers an onRefresh event.

Is FlatList a PureComponent?

flatlist-selectableThis is a PureComponent which means that it will not re-render if props remain shallow-equal. Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData ) that is not === after updates, otherwise your UI may not update on changes.


1 Answers

This also happens if you forget to return something from your renderItem function. For example:

renderItem = (row, sectID, rowID) => {
   <View style={{flexDirection: 'row'}}>
     <Text>{row.item}</Text>
   </View>;
};

As you can see the return statement is missing, but this doesn't trigger an error, warning or anything - you just get a blank Flatlist.

like image 155
JDune Avatar answered Oct 22 '22 03:10

JDune