I understand that the FlatList extraData prop is used to re-render components when it changes.
So, imagine this example:
import React from 'react';
import { View, TouchableOpacity, Text, FlatList } from 'react-native';
export default function App() {
const [selectedId, setSelectedId] = React.useState(null);
const items = [
{
id: 'some-unique-id',
title: 'Item 1',
},
];
const renderItem = ({ item }) => {
return (
<SelectableItem
selected={selectedId === item.id}
onPress={() => setSelectedId(item.id)}
/>
);
};
return (
<FlatList
data={items}
renderItem={renderItem}
keyExtractor={(item) => item.id}
extraData={selectedId} // works the same if I remove this
/>
);
}
function SelectableItem({ selected, onPress }) {
return (
<TouchableOpacity
style={{ backgroundColor: selected ? 'red' : 'white' }}
onPress={onPress}>
<Text>Click Me!</Text>
</TouchableOpacity>
);
}
I have implemented it thinking in re-render the component when the FlatList state changes...
Test it here.
But, the code works the same if I remove the extra data prop... why?