I'm creating an app that has threads of comments (like reddit) and right now the lag is really big.
The really simplified idea is basically:
I have an array
with the id of comments [01, 02, 03, n]
<ParentComponent>
<Flatlist
data={array}
renderItem={(commentId) => <CommentComponent commentId={commentId} />}
/>
</ParentComponent>
Inside <CommentComponent />
I'm making a request with the id and fetching the content to render
const CommentComponent = ({ commentId }) => {
const [comment, setComment] = React.useState({});
useEffect(() => {
const c = getComment(commentId)
setComment(c)
})
return (
<View>
<Text>{comment.text} />
<Flatlist
data={comment.arrayComments}
renderItem={(commentId) => <CommentComponent commentId={commentId} />}
/>
</View>
}
The answer from the call is the text of the comment with the comments to this comment
{
text: 'Lorem ipsum',
comments: [05, 06, 07, 08, n]
}
I'm using recursion to render the comments of the comment, everything works fine except for the absurd lag that I get from rendering a <Flatlist />
inside a <Flatlist />
inside a <Flatlist />
...
And each of the items inside the <Flatlist />
needs to call a render so i'm getting an absurd amount of renders
Some of this threads have 3 digits comments, how can I make this in a more smarter way?
I'm thinking that I have to make this fetchs outside every <CommentComponent />
, but it can take a long time to get all of those comments and it does not feel right.
Anyone has any idea of how can I make this work without getting this absurd lag while scrolling?