Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-array-index-key not working in cloneElement #3213

Closed
lucapollani opened this issue Feb 21, 2022 · 2 comments
Closed

no-array-index-key not working in cloneElement #3213

lucapollani opened this issue Feb 21, 2022 · 2 comments

Comments

@lucapollani
Copy link

lucapollani commented Feb 21, 2022

Following #2083

Hi, I'm currently upgrading my project to React version 16.14.0, so supporting new JSX transform.

I've noticed a change of behavior removing explicit React import.

Considering this snippet of code for example:

Here I've got the "Do not use Array index in keys eslint(react/no-array-index-key)" warning

import React from 'react';
/* [...] */
{tabItems.map((tabItem, tabIndex) =>
	React.cloneElement(tabItem, {
		key: tabIndex
	})
)}

Changing to this:

Here the warning disappears and the code is considered valid.

import { cloneElement } from 'react';
/* [...] */
{tabItems.map((tabItem, tabIndex) =>
	cloneElement(tabItem, {
		key: tabIndex
	})
)}

package.json

"eslint": "~7.32.0",
"eslint-plugin-react": "^7.28.0"

Is this behavior correct?

@ljharb
Copy link
Member

ljharb commented Feb 21, 2022

The warning is correct - indexes should never be used as keys - the bug is that it doesn’t warn in the second example.

@lucapollani
Copy link
Author

Thanks for the feedback @ljharb. Yes I meant that, also to me seemed to be a bug, but preferred to ask to be sure 🙂

@ljharb ljharb closed this as completed in 7fdfd2c Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants