I have a large list, let's say 3k members. I have a component that renders this list and a component for each list item. In the outer component we have code like this:
const list = _.map(this.props.items, (item) => {
return <ListItem key={item.key} {...item} />
});
And then in our JSX we place the list:
<div>
<h3>Check out my sweet list </h3>
{ list }
</div>
Here's the rub: I want to show the user when they click on an item that the item has been selected. So in my ListItem
component I have code that, based on whether or not the individual item has a selected
property, highlights itself. How do I stop React from rerendering the entire list, when only the selected property on a single item changes? I'm sure I need to restructure my code somehow, but I'm not sure what structure will solve this problem. Happy to answer any and all questions and thanks in advance!
Notes:
To optimize React rendering, you need to make sure that components receive only necessary props. It will let you control the CPU consumption and avoid over-rendering unnecessary features. The solution is to create a functional component that will collect all props and redistribute them to other components.
Go to your App component and replace the code with the code below: import React from 'react'; import faker from 'faker' import { FixedSizeList as List } from "react-window"; import './App. css'; function App() { const data = new Array(1000). fill().
React allows us to measure the performance of our apps using the Profiler in the React DevTools. There, we can gather performance information every time our application renders. The profiler records how long it takes a component to render, why a component is rendering, and more.
Okay I figured it out. Successfully implementing shouldComponentUpdate
on each individual item brings a massive performance increase. I had failed to do this for a simple reason.
shouldComponentUpdate(nextProps) {
return !_.isEqual(nextProps, this.props);
}
However... I was doing this on my declaration of each item:
onClick={this.handleItemClick.bind(this, item.key)}
Which mean that this.props.onClick === nextProps.onClick
would always return false, so every single item would always declare itself as needing to be rerendered. After implementing a custom check, it's extremely fast now!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With