question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unnecessary rerenders cause poor performance

See original GitHub issue

Our application has many droppable and draggable components displayed at once and is suffering from poor performance when dragging. When the dragged element crosses over a droppable, it causes all of our draggable and droppable components to rerender for seemingly no reason.

Here’s a code sandbox that demonstrates the issue. Each Draggable/Droppable displays the number of times it has rendered. You’ll notice that when dragging one of the red boxes, the render count of the other Draggable and Droppable components increases even though their props have not changed.

Code Sandbox

The performance of the code sandbox is actually fine, but the unnecessary rerenders cause performance issues in our app because the draggables are expensive to rerender (something we could probably fix).

I’m not sure if this is expected behavior or a bug. If it does seem to be a fixable problem I would be happy to attempt a PR if you provide some direction.

P.S. Great library!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
uinzcommented, Aug 3, 2021

Nice Project!

This is caused by the react context mechanism

https://github.com/clauderic/dnd-kit/blob/master/packages/core/src/components/DndContext/DndContext.tsx#L629

I know there are several ways to avoid rerender

  1. Further subdivide the context, honestly it is difficult
  2. Use unstable API readContext or useContextSelector, avoid rerender through changeBits 3.Like redux, use context as an unchanging subpub center, Use forceUpdate to notify the component update
2reactions
srmaguracommented, Aug 18, 2021

Sounds good. I am pretty sure our issue can be addressed with proper memoization.

Up to you if you want to keep this issue open for when React officially supports useContextSelector or if you want to close it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing React performance by preventing unnecessary re ...
This article explains how to update components only when necessary, and how to avoid common causes of unintentional re-renders.
Read more >
How many re-renders is too many? - Alex Sidorenko
The number of re-renders is a poor performance metric. The long tree of React components that only return JSX can be re-rendered many...
Read more >
Should you optimize every re-render? - Kevin Farrugia
Not all re-renders are bad. Not all re-renders are equal. Some re-renders are more CPU intensive than others. You may debug React re-rendering...
Read more >
How I eliminate ALL unnecessary Rerenders in React - Medium
The most crucial outcome of unnecessary re-renders is when you include infinite loops in your code. This can lead to horrible performance and...
Read more >
Fix the slow render before you fix the re-render - Kent C. Dodds
What is a re-render? · Given that it's slow to update the DOM (like when calling element. · And that performance issue is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found