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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/collections/Breadcrumb/Breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
const dividerKey = `${key}-divider`
sections.forEach(({ text, key, ...restSection }, index) => {
const finalKey = key || text
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.

const dividerKey = `${finalKey}-divider`

sectionsJSX.push(
<BreadcrumbSection {...restSection} key={key}>{text}</BreadcrumbSection>
<BreadcrumbSection {...restSection} key={finalKey}>{text}</BreadcrumbSection>
)

if (index !== sections.length - 1) {
Expand Down