(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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With