react icon indicating copy to clipboard operation
react copied to clipboard

Add more detail to missing-key warning

Open japgolly opened this issue 10 years ago • 7 comments

I have a component in which I must be using an array and not setting keys because I get this warning:

Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of `Blah`.
See https://fb.me/react-warning-keys for more information.

The problem is that render methods aren't always simple and sometimes it's quite hard to find out where the problematic array is.

It would be awesome it the warning could be enhanced to provide some more information about the problematic dom. Something like:

Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of `Blah` when rendering: <ol><li>One</li><li>Two</li></ol>
See https://fb.me/react-warning-keys for more information.

or even

Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of `Blah`.
The head element renders to: <li>One</li>
See https://fb.me/react-warning-keys for more information.

japgolly avatar Mar 02 '16 00:03 japgolly

The same goes for the flattenChildren(...): Encountered two children with the same key warning. Showing a subset of the DOM would be very, very helpful.

japgolly avatar Mar 02 '16 05:03 japgolly

Could you post a plunker or gist with the source example that contains the DOM to which you would want the above warnings to result?

troydemonbreun avatar Mar 17 '16 19:03 troydemonbreun

I believe that a recreation of the issue would be in the following fiddle: https://jsfiddle.net/c8bxbjwv/.

You can see that both "hello" and "world" render, but the world is the invalid element as it doesn't have a key, but the error doesn't display that. I have created an attempt at fixing this with Pull Request #6317.

Please let me know your thoughts

bpedersen avatar Mar 22 '16 06:03 bpedersen

So, given that fiddle, what would the warning text be?

troydemonbreun avatar Mar 22 '16 17:03 troydemonbreun

Currently I have a basic warning,

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of 'Hello'. The head element renders to: <div>world<div/> See https://fb.me/react-warning-keys for more information.

We need to expand on it to cover edge cases, it currently isn't descriptive enough. For example if we have multiple divs with world as text it doesn't distinguish which one is the culprit.

bpedersen avatar Mar 23 '16 03:03 bpedersen

Cool, that would be helpful info.

troydemonbreun avatar Mar 23 '16 17:03 troydemonbreun

This was fixed in https://github.com/facebook/react/pull/6799—now we have an exact stack trace.

gaearon avatar Jun 26 '16 20:06 gaearon