npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

why-did-memo-render

v1.0.16

Published

whyDidMemoRender will give hints about what props triggered a React memo component to render

Downloads

26

Readme

whyDidMemoRender

Find out why your memoized component rendered.

This is a debugging, or diagnostic tool for your development process. It is not intended to be used in Production level code.

Why?

React.memo is a nice way to optimize component rendering. A component wrapped with React.memo will only render again if the props have changed.

However, sometimes a component will render again without developer knowledge. Sometimes it can be an inadvertent props change. It can be hard to figure out which exact props changed. That is why whyDidMemoRender exists.

How to use?

There are two ways. Through a memoProxy, or whyDidMemoRender call. You can view the logs/report by opening up the Dev console.

memoProxy

Use this as the comparison function in place of React.memo's default.

React.memo(() => {
    // component
}, memoProxy);

whyDidMemoRender

For finer control whyDidMemoRender can be called directly with the options set up:

  • tag - The tag to pass for logs
  • displayName - The custom component name
  • prevProps - Previous props
  • nextProps - Next props
React.memo(() => {
    // component
}, (prevProps, nextProps) => {
    whyDidMemoRender("MyTag", "ComponentName", prevProps, nextProps);

    return prevProps === nextProps;
});

Results

Once your scenario has complete, you can view the results in the log. What is important is that the report will detail all props that may have affected render, and the reason why it rendered. Additionally, you can see the diff between objects in stringified form. Stringified because you can then copy and paste to a diffing tool and compare what changed for complex objects.

FAQ

  1. Will whyDidMemoRender help me solve all my rendering issues?

No, the intention for this tool is to help give hints as to why a memo component may be rendering. It may not be exact, but can lead to the right path on how to debug render issues.

  1. Is it only limited to memo components?

Right now, yes. But you can also acquire additional information by wrapping React.memo in a current component and then using memoProxy to see what is causing a rerender on a component that isn't already memo. This will give hints as to what props are triggering renders.

  1. What does it mean when I get the message Could not stringify... in the diff report?

It means the object was too complex. The internal diff tool currently only does a single level comparison.

  1. Is whyDidMemorender always right?

No. This goes back to the 1st point. This tool only gives hints. In order to fully utilize the tool, use it in combination of other render debugging tools like react-devtools at your disposal. Most importantly, understand how React renders components.

I believe with this combination, this tool will be more useful.

Why Did I Render

This tool was inspired by https://github.com/welldone-software/why-did-you-render "why did you render". This one actually directly listens to all React renders and tries its best to give you more than what this tool does. Go check it out!