-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
fix(breadcrumb): do not apply index to component keys #376
Conversation
There is no guarantee that the breadcrumb text is unique. The index provides the extra assurance. |
Current coverage is 92.87% (diff: 100%)
|
@@ -21,12 +21,12 @@ function Breadcrumb(props) { | |||
const dividerJSX = <BreadcrumbDivider icon={icon}>{divider}</BreadcrumbDivider> | |||
const sectionsJSX = [] | |||
|
|||
sections.forEach(({ text, ...restSection }, index) => { | |||
const key = `${text}-${index}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By applying the index to the key we are preventing React from reusing an unchanged component instance in the case that it moved positions. Index as a key is an antipattern, and while this sidesteps the core of that antipattern, it does so by negating the benefit provided in the reconciliation stage.
Text should be a sufficient key for bread crumbs (since each crumb should be unique, otherwise it's nonsensical), but, just in case, I adjusted it to allow for an optional key
property should a user require this use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a happy medium i think. All generated component props should allow passing an object for props. If a user duplicates text in their breadcrumb (or any other component), then they will have to specify their own unique keys.
If that's the case, then this is a 👎 for generating components for users since we can't reliably provide a reusable key (and thus sidestep reconciliation benefits). Again, deny at will, took me all of 2 mins to submit it. I'd just like to see us work more with React than against it. |
I think the utility outweighs the perf/reconciliation benefits. Let's go with the key prop ability you've added and keep the best of both worlds. |
Oh interesting. I was under the impression that we were restricting
@levithomason ? I still think this would have been an issue before this change, but it's a good point you've made @layershifter . |
For initial v1, let's favor simplification. Limit the props to strings only. The sub component markup is available if user's need to use nodes. |
Merge or deny at will, just encountered this while perusing the codebase and figured I'd submit a PR rather than resorting to a slack discussion :).