visx icon indicating copy to clipboard operation
visx copied to clipboard

@vx/tooltip is expensive with renders

Open louispvb opened this issue 7 years ago • 2 comments

withTooltip has no optimizations. Everytime the chart under tooltip needs to update the tooltipData it causes a rerender of the entire chart along with the tooltip. Perhaps it would be better with a react context or small redux state?

louispvb avatar Mar 23 '19 22:03 louispvb

Hi @louispvb, at the moment vx still supports React 15 so we can't move to the "new" Context api released in 16.3 just yet. Another option would be a hook, but same thing as context as hooks were released in 16.8. These would be better solutions out-of-box for vx but until we drop support React 15 we're not ready for them yet.

So, what is the fix to optimize performance and prevent the chart from re-rendering unnecessarily when the tooltip changes? As they are just react components we can use shouldComponentUpdate() and returning false when only the tooltip props have changed.

pseudo code example:

const MyChart = withTooltip(/** ... */);

class ChartComponent extends React.Component {
  shouldComponentUpdate(nextProps) {
    // ...
  }
}

// render
<MyChart>
  <ChartComponent {/** ... */} />
  <TooltipComponent  {/** ... */}  />
</MyChart>

For a more detailed example:

Hope this helps!

hshoff avatar Mar 25 '19 16:03 hshoff

Thanks @hshoff, good advice.

louispvb avatar Mar 28 '19 04:03 louispvb