Reputation:
I have a component that fetches some data like this
const data = fetch(API)
setState([...state, data])
I append new data to the state in a list form. This is how I render my data,
return (state.map(d => <Component data={d}/>))
This is getting really slow when I append more and more data. Is there a way that instead of re-render the existing components, you just append a new component with new data to the existing list of components?
Upvotes: 1
Views: 189
Reputation: 464
Answer really depends on size of dataset:
import React from 'react';
import isEqual from 'react-fast-compare';
const YourComponent = props => {...}
export default React.memo(YourComponent, isEqual);
Upvotes: 1
Reputation: 86
I guess your applications is getting slow, because your component re-renders even if the data stays the same. You could use useMemo/React.memo in your <Component />
to avoid this.
Also you should use a unique key in your state.map
, which is also important to avoid unnecessary re-renders.
Upvotes: 0