I have a case with a Flatlist where users can scroll down to load more rows. I do not know if this is the correct way to check if components are re-rendering, but I added a log statement in the component to see how often it is triggered. Right now, when I load more rows, previous rows are re-rendered, therefore making the process slow as more rows are added.
This is what I am doing so far. I thought using memo would solve this issue, but I don’t think that is working for me. Ideally, I want the componentB to render just once although I am aware useEffect in componentB may cause re-rendering. Even if I am changing totally unrelated state variable, all ComponentB are re-rendered
ComponentA = () => {
const [x, setX] = useState([]);
const [y, setY] = useState(0);
const loadMore = () => {
...load more
setX(newX);
//tested to see if setting y will trigger re-render which it did
setY(1);
}
return <FlatList
data={x}
keyExtractor={item => item.id}
onEndReached={loadMore}
renderItem={({ item }) => { return <ComponentB item={item}> })
>
}
ComponentB = ({item}) => {
useEffect(() => {
fetch some info using item
})
return ...
}
export default memo(ComponentB);
Also, when I navigate to a screen and then come back, componentB are re-rendered although nothing has changed. Note that I tried using memo(ComponentB, customComparator) to check for equality.
2
Answers
If you are in a hurry: https://snack.expo.dev/BItGvjw7S?platform=android
Answer with description
We should move the FlatList into its own component
Then we should wrap our ComponentB with memo
Finally, we should call our MainList component from the App component
To see it in action, go to this link and click the ‘Tap to play’ button please. When the application runs, slowly scroll to the bottom and see console messages on the LOGS tab. If you do not see the LOGS tab, click on the bar with ‘No errors’ text under your browser window to open it up. I think this is what exactly you want.
NOTE: To make things faster, I got the FlatList implementation ready from an answer on StackOverflow and tweaked the code according to your needs.
using memo was a good solution but not enough, you need the remove any anonymous function because it will be recreated on each render which will cause performance issues sometimes.
For example:
Refer to this : https://reactnative.dev/docs/optimizing-flatlist-configuration#avoid-anonymous-function-on-renderitem