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

fix(breadcrumb): do not apply index to component keys #376

Merged
merged 1 commit into from
Aug 5, 2016

Conversation

dvdzkwsk
Copy link
Member

@dvdzkwsk dvdzkwsk commented Aug 5, 2016

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 :).

@levithomason
Copy link
Member

There is no guarantee that the breadcrumb text is unique. The index provides the extra assurance.

@codecov-io
Copy link

Current coverage is 92.87% (diff: 100%)

No coverage report found for master at 64f145a.

Powered by Codecov. Last update 64f145a...ba631fa

@@ -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}`
Copy link
Member Author

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.

Copy link
Member

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.

@dvdzkwsk
Copy link
Member Author

dvdzkwsk commented Aug 5, 2016

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.

@levithomason
Copy link
Member

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.

@levithomason levithomason merged commit 3d24144 into master Aug 5, 2016
@levithomason levithomason deleted the fix/breadcrumb branch August 5, 2016 21:33
@levithomason levithomason mentioned this pull request Aug 8, 2016
3 tasks
@layershifter
Copy link
Member

@davezuko I'm agree with you about key, but how about this? It's correct?

image
image

@dvdzkwsk
Copy link
Member Author

dvdzkwsk commented Aug 9, 2016

Oh interesting. I was under the impression that we were restricting text to a string value and not a React node. However, this same issue would have occured even before this change, would it not? Either way, I can see 4 3 ways we handle this:

  1. Restrict text to a string
  2. Revert the change (ignore this, I don't think it's correct)
  3. If text is not a string, we fall back to the index.
  4. If you do not provide a string text value, you must provide a key

@levithomason ? I still think this would have been an issue before this change, but it's a good point you've made @layershifter .

@levithomason
Copy link
Member

levithomason commented Aug 9, 2016

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.

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

Successfully merging this pull request may close these issues.

4 participants