Reputation: 43
I got those 2 errors in my code, how can I fix those without using "suppress eslint-disable-next-line react/jsx-props-no-spreading/ or eslint-disable-next-line react/jsx-key" for this line?
<UitkTableHead>
{headerGroups.map((headerGroup) => (
<UitkTableRow {...headerGroup.getHeaderGroupProps()}> // Prop spreading is forbidden
{headerGroup.headers.map((column) => (
<UitkTableCell scope="col" {...column.getHeaderProps()}> //Missing "key" prop for element in iterator
{column.render('Header')}
</UitkTableCell>
))}
</UitkTableRow>
))}
I expect to make the code stop showing those errors, how to change the code to make that happened instead of add the ignore comment.
Edit: Here is my rules
"rules": {
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-ignore": "allow-with-description"
}
],
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"class-methods-use-this": "off",
"no-shadow": "off",
"import/no-extraneous-dependencies": "off",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error", {"variables": false}],
"import/extensions": "off",
"react/prop-types": "off",
"react/require-default-props": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"global-require": "off",
"import/no-dynamic-require": "off",
"camelcase": "off",
"react/jsx-props-no-spreading": "off",
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "off",
"@angular-eslint/no-empty-lifecycle-method": "off"
}
Upvotes: 1
Views: 2120
Reputation: 149
Missing "key" prop for element in iterator
This error comes from using the .map()
method on an array in React. When you use .map()
, React wants a way to easily differentiate each item it is mapping when it compares the DOMs. To fix this, all you need to do is add a key
with a unique identifier to each item, and if you don't have a unique identifier, you can add a parameter to .map()
to grab the index of the item it is currently using:
{headerGroup.headers.map((column, index) => (
<UitkTableCell key={index} scope="col" {...column.getHeaderProps()}>
{column.render('Header')}
</UitkTableCell>
))}
Important note, this way of using the index should ONLY be used if you cannot supply a unique key yourself, either through a library that generates one or by giving each object a key
property themselves.
Prop spreading is forbidden
The easiest way to fix that would be to tell ESLint not to consider this file when parsing for that error, but there are different ways:
/* eslint-disable react/jsx-props-no-spreading *
react/jsx-props-no-spreading
return
the mapped arguments that you want rendered:{headerGroups.map((headerGroup) => (
const hgProps = headerGroup.getHeaderGroupProps();
return (
<UitkTableRow {hgProps}>
{headerGroup.headers.map((column, index) => (
<UitkTableCell key={index} scope="col" {...column.getHeaderProps()}>
{column.render('Header')}
</UitkTableCell>
))}
</UitkTableRow>
)
))}
If it happens below at the props for uitkTableCell
as well, you can follow the same rule. Hope this helps!
Upvotes: 1
Reputation: 2311
for Prop spreading is forbidden error refer:
How to resolve eslint error: "prop spreading is forbidden" in a custom route component?
for missing key error: you need to mention a unique value from map returned items in UitkTableCell as <UitkTableCell scope="col" key={column.unique_id}
Upvotes: 0