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

Using Index as a key is an anti-pattern in React #4495

Closed
wants to merge 3 commits into from
Closed

Using Index as a key is an anti-pattern in React #4495

wants to merge 3 commits into from

Conversation

vadimka123
Copy link
Contributor

Fixed issue when on D&D new MultiValue component rendered instead of update exists components
Also in general it's anti-pattern

@changeset-bot
Copy link

changeset-bot bot commented Mar 16, 2021

⚠️ No Changeset found

Latest commit: dcf911b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 16, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit dcf911b:

Sandbox Source
react-codesandboxer-example Configuration

@ebonow
Copy link
Collaborator

ebonow commented Mar 16, 2021

Looks good and makes sense to me though I know this exists in a few other places in Select.js

@@ -1463,7 +1463,7 @@ export default class Select extends Component<Props, State> {
}}
isFocused={isOptionFocused}
isDisabled={isDisabled}
key={`${this.getOptionValue(opt)}${index}`}
key={this.getOptionValue(opt)}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, this was added to fix #4154.

Copy link
Collaborator

@Rall3n Rall3n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would/could introduce a breaking change resulting in Duplicate key warnings.
I believe a better and more robust solution should be considered instead.

@vadimka123
Copy link
Contributor Author

@Rall3n , maybe additional prop ? duplicateValues or maybe pass func for build key ?

Copy link

@NullPainter2 NullPainter2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React-select can't specify key because it does not know what is unique in user's data. Please add callback so user can specify unique ID herself.

/>
))
selectValue.map(opt => {
const value = this.getOptionValue(opt);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value might not be unique at all for key.

Make this customizable by user instead by some callback, like this.getReactKey(opt,i), because I as a user know how to specify unique id for my data.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rall3n Is "value" of option required to be unique or not ? I assume it should be ... but I found no information about that in docs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NullPainter2 it's likely assumed, but not sure that it's a stated requirement. Perhaps something to consider. I wonder if something like ${label}_${value} would be acceptable as I can't think of a compelling reason why or how a user would have duplicate derived value and label values. Perhaps in different option groups, but they would have a different parent node so the keys I believe wouldn't need to be unique.

@ebonow
Copy link
Collaborator

ebonow commented Mar 18, 2021

Greetings @vadimka123 ,

Thank you for putting this together. We discussed this with Jed and the rest of the team, and this is something we would certainly like to address, but given that there is no requirement for values to be unique, this could potentially be a breaking change.

We would likely would want a more complete solution perhaps as @NullPainter2 had suggested with a new built-in prop for more advanced use cases like this. We want to address this, but are currently in the middle of a total rewrite to TypeScript. This will be something that we will revisit once it's complete,

@pgmanutd
Copy link

pgmanutd commented May 25, 2021

One issue got raised today around this I think.

@SalahAdDin
Copy link

@vadimka123 Can you solve it, please?

@dcousens dcousens added this to the 6.0 milestone Oct 31, 2022
@vadimka123 vadimka123 closed this by deleting the head repository Jan 16, 2024
@SalahAdDin
Copy link

@vadimka123 what?

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.

None yet

8 participants