display-name
Configuration
Rule Details
Disallow missing displayName in a React component definition.
displayName allows you to name your component. This name is used by React in debugging messages.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Rule Options
ignoreTranspilerName (default: false)
When true the rule will ignore the name set by the transpiler and require a displayName property in this case.
Examples of correct code for this rule with { ignoreTranspilerName: true }:
Examples of incorrect code for this rule with { ignoreTranspilerName: true }:
checkContextObjects (default: false)
When true, this rule will also warn on context objects (created via React.createContext() / createContext()) that don't have a displayName set.
This option is silently disabled when settings.react.version is below 16.3.0, since Context.displayName was introduced in React 16.3.
Examples of incorrect code for this rule with { checkContextObjects: true }:
Examples of correct code for this rule with { checkContextObjects: true }:
About component detection
For this rule to work we need to detect React components, this could be very hard since components could be declared in a lot of ways.
For now we detect components created with:
createReactClass()- an ES6 class that inherits from
React.Component/Component/PureComponent - a stateless function that returns JSX or the result of a
React.createElementcall React.memo/React.forwardRef(or destructuredmemo/forwardRef) wrapping any of the above
Settings
This rule is influenced by the shared React settings:
settings.react.pragma— theReactobject name used for<pragma>.Component/<pragma>.createContext/<pragma>.memo/<pragma>.forwardRefmatching (default:"React").settings.react.createClass— thecreateReactClassfactory name used for ES5 component detection (default:"createReactClass").settings.react.version— gates the version-dependent behaviors:- Nested
<pragma>.memo(<pragma>.forwardRef(...))is allowed for^0.14.10 || ^15.7.0 || >= 16.12.0(default: latest). checkContextObjectsis silently disabled when below16.3.0.
- Nested
settings.componentWrapperFunctions— additional wrapper factories beyondmemo/forwardRef. Accepts strings ("observer") or{ property, object }entries. The<pragma>placeholder is substituted with the configured pragma.
When Not To Use It
If you are not interested in giving every component a displayName and rely on bundler / transpiler-derived names for debugging, you can disable this rule.