Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Native FlatList refuses to render content

I’ve read tens of threads on FlatList, and I can’t seem to get this to work. This is extremely basic so I must be missing something rudimentary. Why is this returning a blank screen?

import React from "react";
import { View, Text, FlatList } from "react-native";

export default function App() {
  const renderItem = ({ item }) => {
    console.log(item);
    return (
      <View>
        <Text>{item.title}</Text>
      </View>
    );
  };
  const keyExtractor = (item) => item.id;

  return (
    <View>
      <FlatList>
        data=
        {[
          { id: 1, title: "1" },
          { id: 2, title: "2" }
        ]}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      </FlatList>
    </View>
  );
}

https://codesandbox.io/s/quirky-chandrasekhar-jgjj28?file=/src/App.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code has an error data, keyExtractor, renderItem are props for the FlatList you use them like a text/children

it must look like this:

import React from "react";
import { View, Text, FlatList } from "react-native";

export default function App() {
  const renderItem = ({ item }) => {
    console.log(item);
    return (
      <View>
        <Text>{item.title}</Text>
      </View>
    );
  };
  const keyExtractor = (item) => item.id;

  return (
    <View>
      <FlatList
        data=
        {[
          { id: 1, title: "1" },
          { id: 2, title: "2" }
        ]}
        renderItem={renderItem}
        keyExtractor={keyExtractor}>
      </FlatList>
    </View>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading