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

multiple children with same key: warning message prepends '.$' to actual key value #6475

Closed
go-oleg opened this issue Apr 10, 2016 · 2 comments

Comments

@go-oleg
Copy link

go-oleg commented Apr 10, 2016

If multiple children have the same key, for example (jsfiddle):

var Hello = React.createClass({
    render: function() {
        return (<ul>
        <li key={"1"}>what</li>
        <li key={"1"}>what</li>
        </ul>
        )
    }
});

React prints the following warning to the console:

Warning: flattenChildren(...): Encountered two children with the same key, `.$1`. Child keys must be unique; when two children share a key, only the first child will be used.

The key referenced in the warning prepends an internal-looking '.$' to the key specified by the user. This is a bit misleading - ideally, it would mention the exact key value set by the user.

@jimfb
Copy link
Contributor

jimfb commented Apr 11, 2016

User-specified keys undergo an escaping transformation to prevent collisions with automatically-generated keys. For instance, if your li element had the key 1=::=2, the key in the warning would be .$1=0=2=2=02. This happens during traversal, so flattenChildren gets the already-escaped keys and doesn't have access to the original keys.

But yes, auto-generated keys are completely internal (never user visible) and are guaranteed to never conflict (because they are generated, and user keys are escaped), so I don't see a reason why we couldn't report the original key name in warning messages.

Probably the best solution would be to un-escape the key before reporting the error message. We would want to move key escaping/unescaping into a KeyEscapeUtils utility that provides an escapeKey and unescapeKey function.

@hkal
Copy link
Contributor

hkal commented Apr 11, 2016

If no one minds, I'd like to give this a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants