Reputation: 198
I have a large list of items to render and list data get updated in realtime with socket-io. How to handle performance issues? it gets updated heavily and table of items rendered so many times.
Upvotes: 4
Views: 4049
Reputation: 385
I was in the same boat and solved my problem a different way, by implementing the shouldComponentUpdate
lifecycle method in my component:
shouldComponentUpdate(nextProps) {
// Note: lodash's "isEqual" method is useful for performing a deep comparison
// of the given objects, which is more than JavaScript can do natively:
return !_.isEqual(this.props.listItems, nextProps.listItems);
}
This is perhaps a better overall solution, because:
React.memo()
HOCUpvotes: 0
Reputation: 9812
There are a couple of things to consider:
For #1 you can try using a windowing library such as react-virtualized, this will make the burden on the DOM much lighter, instead of rendering thousands of items, you will actually be rendering only a few tens.
For #2 make sure that when an item updates, only that items updates and not the list. You can use memo to ensure your list items are only renders again when their props change.
Upvotes: 3