Eslint-plugin-react: react/no-array-index-key - Breaking this.props.children in to chunks and using index as a key

Created on 23 Mar 2017  路  4Comments  路  Source: yannickcr/eslint-plugin-react

Are there circumstances when it is ok to use array index as a key? If so, could these exceptions be added to the documentation here:

https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md

I have run in to an issue where I am breaking down this.props.children in to several chunks in order to render them in a grid - but I can't see how I could use anything other than the array index as an identifier - more information here:

http://stackoverflow.com/questions/42983067/breaking-children-in-to-chunks-and-react-no-array-index-key

question

Most helpful comment

You can use simple 'hack'
<span> key={String(index)} </span>
And you don't pollute your code with inline disabled.

All 4 comments

If there's nothing unique about the items, then you'd simply disable the rule with an override.

@penx If you have a static set it should be ok to use the array index as values. If you have other features (eg. filtering) then it's recommended to use a unique key to avoid index collisions since React uses keys first to check if a node should be updated.

You can use simple 'hack'
<span> key={String(index)} </span>
And you don't pollute your code with inline disabled.

That鈥檚 just defeating the purpose of the rule. Might as well turn it off.

Was this page helpful?
0 / 5 - 0 ratings