no-missing-component-display-name
Enforces that all components have a 'displayName' that can be used in DevTools.
Full Name in eslint-plugin-react-x
react-x/no-missing-component-display-nameFull Name in @eslint-react/eslint-plugin
@eslint-react/no-missing-component-display-nameRule Details
When defining a React component, if you specify its component name in a way that React can't infer its displayName, it will show up as an anonymous component in DevTools, which makes debugging more difficult.
Examples
Wrapping an anonymous arrow function with React.memo
// Problem: React cannot infer displayName from an anonymous arrow function; it will show as anonymous in DevTools
import React from "react";
const Button = React.memo(() => <div />);
// ^^^ Add missing 'displayName' for component.// Recommended: Use a named function or manually add displayName after assignment
import React from "react";
const Button = React.memo(function Button() {
return <div />;
});import React from "react";
const Button = React.memo(() => <div />);
Button.displayName = "Button";Wrapping an anonymous arrow function with React.forwardRef
// Problem: When forwardRef wraps an anonymous function, React cannot automatically infer the component name
import React from "react";
const Button = React.forwardRef(() => <div />);
// ^^^ Add missing 'displayName' for component.// Recommended: Use a named function or manually set the displayName property
import React from "react";
const Button = React.forwardRef(function Button() {
return <div />;
});import React from "react";
const Button = React.forwardRef(() => <div />);
Button.displayName = "Button";Default exporting an anonymous arrow function
// Problem: When default-exporting an anonymous function, the component has no recognizable name in DevTools
import React from "react";
export default () => <div />;
// ^^^ Add missing 'displayName' for component.// Recommended: Change the default export to a named function declaration
import React from "react";
export default function Button() {
return <div />;
}Versions
Resources
Further Reading
See Also
react-x/no-missing-context-display-name
Enforces that all contexts have adisplayNamethat can be used in DevTools.