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.

`displayName` from memoized components should be taken into account in DevTools

See original GitHub issue

If you create a component via React.memo and then explicitly provide it with a displayName, DevTools ignore this display name.

React version: 16.12.0 DevTools version: 4.4.0

Steps To Reproduce

  1. Create a component like:
const FancyMemoComponent = React.memo(() => 'Check my name in DevTools')
  1. Provide it with a displayName
FancyMemoComponent.displayName = 'FancyMemoComponent'
  1. Check its name in DevTools.

Link to code example: https://codesandbox.io/s/react-memo-display-name-vk7gv

The current behavior

DevTools do not display the given displayName.

The expected behavior

DevTools should display the given displayName.

Comments

I am aware that the name can be picked from the function inside React.memo, but I prefer using anonymous functions.

I am using this workaround:

FancyMemoComponent.type.displayName = 'FancyMemoComponent'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:10

github_iconTop GitHub Comments

7reactions
moonrailguncommented, Feb 26, 2020

I have a alternative solution to fix it by myself when React have not fix it

import React, { PropsWithChildren, SFC } from 'react';

export const MyMemo = <P extends object>(
  Component: SFC<P>,
  propsAreEqual?: (
    prevProps: Readonly<PropsWithChildren<P>>,
    nextProps: Readonly<PropsWithChildren<P>>
  ) => boolean
) => {
  const memoized = React.memo(Component, propsAreEqual);

  Object.defineProperty(memoized, 'displayName', {
    set(v) {
      Component.displayName = v;
    },
  });

  return memoized;
};

Its easy to replace and not change any api

6reactions
Scalahansolocommented, Jun 10, 2022

This is still an issue as outlined by the linked sandbox. Not solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When is it required to manually set the displayName for a ...
When I used the React dev tools to profile my inputs I found that each input had the same display name as the...
Read more >
How To Debug React Components Using React Developer ...
It is not necessary to add a displayName , but it does help to navigate components when analyzing the component tree in the...
Read more >
Avoid anonymous components with `displayName` - Jules Blom
Context objects have a displayName property that you can set to a helpful name and React will use it in debugging messages. The...
Read more >
How to settle the React memoization debate - Medium
React's memo utility allows a developer to control when a component will re-render based on its props. It's a direct replacement for the ......
Read more >
Faster React Apps With Memoization - Pixie Labs Blog
TL;DR: Most of our React components were re-rendering too often. Memoizing them fixed it. We used React Dev Tools and Why Did You...
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