[Bug]: react/no-array-index-key
falsely triggered for Map.prototype.forEach()
#3670
Description
Is there an existing issue for this?
- I have searched the existing issues and my issue is unique
- My issue appears in the command-line and not only in the text editor
Description Overview
When using the forEach
method on a Map, the second parameter given to the callback function is not an index, but rather a key of the map. I believe this is quite different from a typical array index and doesn't align with the reasons why react/no-array-index-key
exists, and it isn't really displayed in the example document as well.
For example, some code like this triggers the warning:
const map = new Map();
...
return (<div>
{map.forEach((item, key)=><div key={key}>{item}</div>)}
</div>)
or
const map = new Map();
...
const results = [];
// react/no-array-index-key triggered here
map.forEach((item, key) => results.push(<div key={key}>{item}</div>));
return <div>{results.map((item) => item)}</div>;
Whereas I think it is reasonable to assume the key given here is a unique ID that is different from a typical array index, since it is a map, therefore giving it a warning by the name no-array-index-key
is quite unintuitive.
Expected Behavior
With the same code above, there should be no warning regarding the key props inserted in the return statement of the Map.prototype.forEach()
call, or at the very least a warning with a different description to not give it an array-specific warning title.
eslint-plugin-react version
v7.28.0
eslint version
v8.2.0
node version
v18.15.0
Activity