Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - FlatList Not Rendering

(Note: I'm using Expo for this app)

I'm attempting to render a FlatList that displays a list of printers. Here's the code:

<FlatList
  data={printers}
  keyExtractor={printer => printer.id}
  renderItem={({ item }) => {
    return (
      <Printer
        printerTitle={item.name}
        selected={item.selected}
        last={item === last(printers)}
      />
    );
  }}
/>

Here's the code for the <Printer /> component:

const Printer = props => {
  const { last, printerTitle, selected } = props;
  return (
    <View style={[styles.container, last ? styles.lastContainer : null]}>
      <View style={styles.innerContainer}>
        <View style={styles.leftContainter}>
          {selected ? (
            <Image source={selected ? Images.checkedCircle : null} />
          ) : null}
        </View>
        <View style={styles.printerDetails}>
          <Text style={styles.printerTitle}>{printerTitle}</Text>
        </View>
      </View>
    </View>
  );
};

...

export default Printer;

I can't seem to get the <Printer /> component to render. I have tried including a similar custom component (that has worked in a FlatList in another part of the app) in the renderItem prop, and it doesn't work either.

However, when I replace the <Printer /> component with <Text>{item.name}</Text> component, the printer name renders like I would expect it to.

Has anyone run into this issue before, or does anyone have a solution?

like image 235
makozlo Avatar asked Nov 01 '17 19:11

makozlo


People also ask

Why is FlatList not rendering?

flatlist-selectableInternal state is not preserved when content scrolls out of the render window. Make sure all your data is captured in the item data or external stores like Flux, Redux, or Relay. This is a PureComponent which means that it will not re-render if props remain shallow-equal.

Does FlatList render all items?

maxToRenderPerBatch​ It is a VirtualizedList prop that can be passed through FlatList . This controls the amount of items rendered per batch, which is the next chunk of items rendered on every scroll.

What is render item in FlatList?

Using the FlatList component renderItem is a function that requires returning the component that will display the data in the list. keyExtractor – This is a prop responsible for taking an individual item of our data as an argument and returning a value used as a key. The id will be used as a key.


1 Answers

In my case, where I'm rendering a custom component for each item in the list, it wasn't rendering because I accidentally had {} surrounding the return part of the renderItem prop instead of ().

Changing:

<FlatList
  data={array}
  renderItem={({item, index}) => { <CustomItemComponent /> }}
/>

to this:

<FlatList
  data={array}
  renderItem={({item, index}) => ( <CustomItemComponent /> )}
/>

Solved my issues.

like image 178
Jim Avatar answered Oct 09 '22 09:10

Jim