-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Item: chance of auto-generated key collision when using shorthand props #2418
Comments
If you have duplicated shorthand values, pass props objects and include unique keys. Alternatively, you can use the full markup. Shorthand is a convenience and it cannot cover every use case. |
I understand that, however if you're dynamically generating your props you might have no idea that there's a possibility of having duplicate keys until you happen to feed it the right data (or maybe not at all if the warning isn't shown in production, which could potentially result in insidious bugs). It's the fact that it's surprising/not easily predictable that makes it an issue. I'm sure there's a good underlying reason for
|
TL;DR 👍 PR for a 👎 All other proposals above. I will take some time to clarify and recap our history so I can link to it later :)
We used to enable I'd gladly merge a PR for this!
Yes, don't use shorthand strings/numbers in cases where it might be brittle. Use props objects or the full markup.
Unfortunately, none of these options will work:
I can see how it would feel that way but I assure you we aren't copping out on you. In React 102 and 103 we learn that keys must also be stable and predictable. Per the reconciliation docs: "Keys should be stable, predictable, and unique. Unstable keys (like those produced by A key must be unique among its siblings, it must not change over time, and you must be able to keep it assigned to the same component between renders even when that component's props and position in the DOM change drastically. In short, there is no automatic way to do this. Hence, React requires user-defined keys.
Factories have a rich history of iteration. Fix-all solutions to handling keys seem obvious, however, the devil is in the details. We've gone through iterations of not handling keys at all, generating keys for all shorthand values, generating keys only for primitive values, generated keys behind a Why not generate random keys?Random keys, like indexes, are an anti-pattern and will break React. https://reactjs.org/docs/reconciliation.html Why not use a counter for keys?This ends up being the same as using an index or random key. Though this key is unique and constant, it is not permanent. There is no way to know which position in the DOM a node has moved to in order to reassign it's key to it. Why remove the
|
Thanks for taking the time to write a clear and detailed explanation, that all makes sense. I'll throw together that PR. It might take a little time to go through and find all the appropriate |
Awesome, that would be very much appreciated! Once the argument is added and tested, we can certainly help identify and update Almost all our usages will be able to take advantage of this. There are very few components that use the factories in arrays. Just be sure to create a branch on your fork instead of using your master branch. This way, we can push changes to help out. |
Steps
Expected Result
No issues.
Actual Result
The warning is caused by the keys being automatically set from the value, here:
Semantic-UI-React/src/lib/factories.js
Lines 89 to 92 in 9ef4156
Note that there's no need for keys at all in this example.
One workaround that prevents the keys from being set (and therefore the error):
Version
0.77.1
Testcase
https://codesandbox.io/s/4r702vq8y4
The text was updated successfully, but these errors were encountered: