Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance when highlighting item in list with React

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:

  • I'm not saying rendering 3k items in a list is good, just believe this should be, in principle, possible to do with react.
  • The list isn't being literally re-rendering on the actual dom, but the virtual dom is doing the work of comparing everything, and that takes a lot of time.
like image 613
matty-d Avatar asked Oct 23 '15 15:10

matty-d


People also ask

What makes ReactJS performance faster?

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.

How do you optimize a rendering list in React?

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().

How do you check the performance of a React application?

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.


1 Answers

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!

like image 106
matty-d Avatar answered Oct 18 '22 20:10

matty-d