Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Component definition is missing display name react/display-name

Tags:

reactjs

eslint

How do I add a display name to this?

export default () =>
  <Switch>
    <Route path="/login" exact component={LoginApp}/>
    <Route path="/faq" exact component={FAQ}/>
    <Route component={NotFound} />
  </Switch>;
like image 971
David Avatar asked Oct 25 '18 15:10

David


People also ask

Is Missing display name React display name?

Set the displayName property on the component to fix the "Component definition is missing display name" error, e.g. App. displayName = 'MyApp'; . Alternatively, disable the ESLint rule for a line with the following comment - // eslint-disable-next-line react/display-name . Copied!

What is displayName in React?

DisplayName allows you to name your component. This name is used by React in debugging messages.

How do you get the React component name?

To get a component's name in React, we can use the displayName property. to set the Foo. displayName property to the name we want. Then we can retrieve it in the code and the React developer tools.

What is functional component in Reactjs?

A React functional component is a simple JavaScript function that accepts props and returns a React element. After the introduction of React Hooks, writing functional components has become the ​standard way of writing React components in modern applications.


6 Answers

Exporting an arrow function directly doesn't give the component a displayName, but if you export a regular function the function name will be used as displayName.

export default function MyComponent() {
  return (
    <Switch>
      <Route path="/login" exact component={LoginApp}/>
      <Route path="/faq" exact component={FAQ}/>
      <Route component={NotFound} />
    </Switch>
  );
}

You can also put the function in a variable, set the displayName on the function manually, and then export it.

const MyComponent = () => (
  <Switch>
    <Route path="/login" exact component={LoginApp}/>
    <Route path="/faq" exact component={FAQ}/>
    <Route component={NotFound} />
  </Switch>
);

MyComponent.displayName = 'MyComponent';

export default MyComponent;
like image 52
Tholle Avatar answered Oct 02 '22 14:10

Tholle


tldr: switch arrow function to a named function

Lint Error shown: Component definition is missing display name react/display-name.

To resolve, you can name your function (IOW, do not use arrow function). In this example, I am using react-table and passing a custom component to render in a cell.

No error:

{
  Cell: function OrderItems({ row }) {
    return (
      <a>
        View Items
      </a>
    );
  },
}

Error:

{
  Cell: ({ row }) => (
    <a>
      View Items
    </a>
  )
}
like image 42
DeBraid Avatar answered Oct 02 '22 14:10

DeBraid


A way to add displayName property to anonymous component function without creating named function is to use recompose:

import { compose, setDisplayName } from 'recompose';

export default compose(setDisplayName('SomeComponent'))(props => ...);

Or just:

export default Object.assign(props => ..., { displayName: 'SomeComponent' });
like image 43
Estus Flask Avatar answered Oct 02 '22 12:10

Estus Flask


Writing the follow line, before the "arrow function" it worked for me

> /* eslint-disable react/display-name */
Header: () => <strong>ID</strong>,

Look how work here: https://eslint.org/docs/latest/user-guide/configuring/rules

like image 37
Z rf Avatar answered Sep 16 '22 23:09

Z rf


Similarly if you have a functional component like:

export default function test(){
    

return (<div></div>

}

And make another component inside of that like a text box which updates a state inside of a functional component from using refs which makes sure the entire page does not re-render and only just that component you need to give it a display name. Or there will be build errors.

export default function test(){
    const stateForFunctionalComponent = useRef("");

    const seperateTextBoxState = React.forwardRef((props,ref) => {
    const [textBoxDocTitle, setTextBoxDocTitle] = useState("");
    
    useEffect(() => {
      ref.current = textBoxDocTitle;
    },[textBoxDocTitle])
  
    return <input 
      id="somethingUnique" 
      type="text" 
      required="required" 
      placeholder="Enter document name..." 
      onInput={(e) => setTextBoxDocTitle(e.target.value)}>
  </input>})

    //Line that matters!!!
    seperateTextBoxState.displayName = "seperateTextBoxState";
}

    return (<div><seperateTextBoxState ref={stateForFunctionalComponent}/></div>)
}
like image 32
kpgabriel Avatar answered Oct 02 '22 14:10

kpgabriel


if you are using eslint this may be because of don't providing the following code yourclassName.displayName="name"

like image 44
john babu Avatar answered Oct 02 '22 13:10

john babu