eslint-plugin-react
eslint-plugin-react copied to clipboard
`react/display-name` and `react/prop-types` are incorrectly reported in `react-table`
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?
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.
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?
No, you can use a normal named function instead of an arrow - components shouldn’t be arrow functions, ideally.
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;
True - for that you’d have to extract it to a separate variable and add .propTypes.
The display-name tests do not warn on the latest default branch; the prop-types ones are still warning.
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 the latest release of the plugin should fix the display-name issues. What version are you using?
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