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

(react/jsx-key) still warns when wrapping with React.Children.toArray #1574

Closed
hamlim opened this issue Nov 29, 2017 · 7 comments · Fixed by #1591
Closed

(react/jsx-key) still warns when wrapping with React.Children.toArray #1574

hamlim opened this issue Nov 29, 2017 · 7 comments · Fixed by #1591

Comments

@hamlim
Copy link

hamlim commented Nov 29, 2017

The (react/jsx-key) rule seems to still warn for instances wrapped with the React.Children.toArray utility.

This seems to be a bug since React no longer warns about missing keys when wrapping and considers it valid (the toArray utility provides keys to each item automatically)

// React and lint rule both warn
{
  things.map(item => <Thing {...item} />)
}

// lint rule warns but React considers this to be valid
{
  React.Children.toArray(
    things.map(item => <Thing {...item} />)
  )
}

Suggested Solution:

The rule accounts for instances wrapped with React.Children.toArray correctly and does not warn.

Edit:
I incorrectly described this as the no-array-index (as key) rule from this eslint plugin, but it is actually: [eslint] Missing "key" prop for element in iterator (react/jsx-key). Updated the body of the issue plus the title to reflect the correct rule.

@hamlim hamlim changed the title no-array-index still warns when wrapping with React.Children.toArray no-array-index still warns when wrapping with React.Children.toArray Nov 29, 2017
@hamlim hamlim changed the title no-array-index still warns when wrapping with React.Children.toArray (react/jsx-key) still warns when wrapping with React.Children.toArray Dec 4, 2017
silvenon added a commit to silvenon/eslint-plugin-react that referenced this issue Dec 10, 2017
jsx-key rule should always succeed if we're inside
React.Children.toArray() because omitting the key there doesn't cause a
React warning.

Fixes jsx-eslint#1574.
@ljharb
Copy link
Member

ljharb commented Dec 10, 2017

@hamlim @silvenon in which version of React did it stop warning about keys inside React.Children?

@silvenon
Copy link
Contributor

No idea, my guess is that it's worked that way forever (ever since that API was introduced).

@ljharb
Copy link
Member

ljharb commented Dec 10, 2017

As long as React 0.13 also didn't warn, then I think we're good :-) otherwise we'd need to warn on older React versions.

@hamlim
Copy link
Author

hamlim commented Dec 10, 2017

Did some quick testing, going back to 0.14.0 it still doesn't warn: https://codesandbox.io/s/92rp6893po

@hamlim
Copy link
Author

hamlim commented Dec 10, 2017

And it seems like toArray didn't exist on React.Children in 0.13.0.

@MatPayette
Copy link

Is this still a thing after 5 years? If not, what's the solution?

@silvenon
Copy link
Contributor

@MatPayette not sure, you can easily check, and if it's still an issue you can try continuing my PR #1591 based on the comments. 🙂

ljharb pushed a commit to silvenon/eslint-plugin-react that referenced this issue Sep 30, 2022
jsx-key rule should always succeed if we're inside
React.Children.toArray() because omitting the key there doesn't cause a
React warning.

Fixes jsx-eslint#1574.
ljharb pushed a commit to silvenon/eslint-plugin-react that referenced this issue Sep 30, 2022
jsx-key rule should always succeed if we're inside
React.Children.toArray() because omitting the key there doesn't cause a
React warning.

Fixes jsx-eslint#1574.
@ljharb ljharb closed this as completed in 8beb2aa Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants