welldone-software/why-did-you-render

Feature Request: Support custom equality checks

Open

#319 创建于 2025年5月8日

在 GitHub 查看
 (1 评论) (1 反应) (0 负责人)JavaScript (10,560 star) (188 fork)batch import
bugenhancementgood first issuehelp wantedhigh priority

描述

My app makes heavy use of custom equality checks for props and state via shouldComponentUpdate for class components and the arePropsEqual argument of React.memo for function components.

When I first tried out why-did-you-render I got a lot of notifications of props changing that were deeply equal that I knew were not actually causing re-renders (the components are re-rendering, but due to other reasons) because they have custom equality checks.

For now I've just patched why-did-you-render in logDifference to filter out differences that pass my custom equality check (if there's a .equals method on the object, I use that).

But I'd love it if there were support for custom equality checks. I think arePropsEqual should be possible to support automatically. shouldComponentUpdate is obviously more complicated, but I could imagine an advanced tracking setting that takes a callback:

static whyDidYouRender = {
  arePropsEqual: (oldProps, newProps) => ...  // comparison here
}

Technically there's already an escape hatch via a custom notifier but then you have to re-implement all of the default notifier logic yourself. I suppose an alternative solution would be to make the notifier exports more modular, such that filtering out differences is an easy thing to do without patching.

贡献者指南

Feature Request: Support custom equality checks · welldone-software/why-did-you-render#319 | Good First Issue