eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

`react/display-name` and `react/prop-types` are incorrectly reported in `react-table`

Open garyking opened this issue 6 years ago • 9 comments

For this component:

import React from 'react';
import ReactTable from 'react-table';

import 'react-table/react-table.css';

function Test() {
  const data = [
    {
      name: 'Bob',
    },
  ];

  const columns = [
    {
      Header: 'Name',
      accessor: 'name',
      Cell: ({ value }) => <div>{value}</div>,
    },
  ];

  return <ReactTable columns={columns} data={data} />;
}

export default Test;

The line: Cell: ({ value }) => <div>{value}</div>, incorrectly reports the following two errors:

Component definition is missing display name. eslint(react/display-name)
'value' is missing in props validation. eslint(react/prop-types)

These two errors should not appear. There are, correctly, no errors, when the above code is converted to a class:

import React from 'react';
import ReactTable from 'react-table';

import 'react-table/react-table.css';

class Test {
  render() {
    const data = [
      {
        name: 'Bob',
      },
    ];

    const columns = [
      {
        Header: 'Name',
        accessor: 'name',
        Cell: ({ value }) => <div>{value}</div>,
      },
    ];

    return <ReactTable columns={columns} data={data} />;
  }
}

export default Test;

Otherwise, is there a way I can avoid these errors, without disabling the rules?

garyking avatar Sep 08 '19 06:09 garyking

That's because it's a component. It should be warned on both of those rules.

The bug, in fact, is that it's not warned in a class.

ljharb avatar Sep 08 '19 06:09 ljharb

Okay. Should I keep this issue open until both rules are fixed for within classes? Here is my config for both rules:

{
  "react/display-name": [
    "error",
    {
      "ignoreTranspilerName": false
    }
  ],

  "react/prop-types": [
    "error",
    {
      "ignore": [],
      "customValidators": [],
      "skipUndeclared": false
    }
  ]
}

Is the only way to resolve both rules, is to pull out the component and assign it to a variable, then assign that variable to the object key, so that the display name is automatically inferred from the variable name, and then I can assign prop types to it like I normally would?

garyking avatar Sep 08 '19 17:09 garyking

No, you can use a normal named function instead of an arrow - components shouldn’t be arrow functions, ideally.

ljharb avatar Sep 08 '19 17:09 ljharb

I tried the following, and it did indeed resolve display-name, but not prop-types:

import PropTypes from 'prop-types';
import React from 'react';
import ReactTable from 'react-table';

import 'react-table/react-table.css';

function Test() {
  const data = [
    {
      name: 'Bob',
    },
  ];

  const columns = [
    {
      Header: 'Name',
      accessor: 'name',
      Cell({ value }) {
        return <div>{value}</div>;
      },
    },
  ];

  columns[0].Cell.propTypes = {
    value: PropTypes.string.isRequired,
  };

  return <ReactTable columns={columns} data={data} />;
}

export default Test;

garyking avatar Sep 08 '19 18:09 garyking

True - for that you’d have to extract it to a separate variable and add .propTypes.

ljharb avatar Sep 08 '19 19:09 ljharb

The display-name tests do not warn on the latest default branch; the prop-types ones are still warning.

ljharb avatar Feb 19 '22 00:02 ljharb

I've also noticed this in our code. The code is this:

export const isDangerouslySetClassNamesType =
  (keys: string[], complexKeys?: string[]) =>
  (props: { [key: string]: any }, propName: string, componentName: string) => {
...

It's a function and not a component but the error is error Component definition is missing display name react/display-name on the last line above

WayneEllery avatar Jun 22 '22 06:06 WayneEllery

@WayneEllery the latest release of the plugin should fix the display-name issues. What version are you using?

ljharb avatar Aug 05 '22 05:08 ljharb

Thanks. Yes that comment was 22nd of June so an older version. This is now no longer an issue for me. Thanks for fixing it and letting me know

WayneEllery avatar Aug 16 '22 22:08 WayneEllery