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

Rule idea: jsx-unique-key #2614

Closed
silverwind opened this issue Apr 2, 2020 · 12 comments
Closed

Rule idea: jsx-unique-key #2614

silverwind opened this issue Apr 2, 2020 · 12 comments

Comments

@silverwind
Copy link
Contributor

silverwind commented Apr 2, 2020

I'd like to propose a rule that triggers when duplicate key properties are defined in on the same JSX nesting level or in arrays containing JSX at the top-level element.

React depends on unique keys and will render very unexpected results when duplicate keys are present which shoult to my knowledge almost never be intentional.

Examples where the rule would trigger an error:

const div = (
  <div>
    <span key="notunique"/>
    <span key="notunique"/>
  </div>
);
const spans = [
  <span key="notunique"/>,
  <span key="notunique"/>,
];
@ljharb
Copy link
Member

ljharb commented Apr 2, 2020

That seems like something jsx-key should report on, either by default or behind an option.

@ljharb
Copy link
Member

ljharb commented Feb 19, 2022

The first example shouldn't warn because key is irrelevant there, because it's not inside an array.

@ljharb
Copy link
Member

ljharb commented Feb 19, 2022

I'm going to solve this by adding a warnOnDuplicates option.

@ljharb ljharb closed this as completed in dbc8e8c Feb 22, 2022
@silverwind
Copy link
Contributor Author

The first example shouldn't warn because key is irrelevant there, because it's not inside an array.

I think any HTML sibling nodes can be affected by key conflicts.

@ljharb
Copy link
Member

ljharb commented Feb 22, 2022

@silverwind can you provide a react codesandbox that illustrates such a problem? if so, I can try to include it in the warnOnDuplicates option prior to release.

@silverwind
Copy link
Contributor Author

silverwind commented Feb 23, 2022

I think the worst that can happen with non-array cases is unnecessary re-renders. That said, React does warn at runtime for the first example I provided above.

Warning: Encountered two children with the same key, notunique. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

React docs say:

The key only has to be unique among its siblings, not globally unique.

@ljharb
Copy link
Member

ljharb commented Feb 23, 2022

Ok thanks, I’ll reopen this then to cover the first case with the new option.

@ljharb ljharb reopened this Feb 23, 2022
@ljharb ljharb closed this as completed in f51ec45 Feb 23, 2022
@tido64
Copy link

tido64 commented Feb 25, 2022

@ljharb: Since f51ec45, jsx-key is now being triggered for all elements inside a React.Fragment. Is that intentional? Our CI fails with 7.29: https://github.com/microsoft/rnx-kit/runs/5333921004?check_suite_focus=true

The code that it trips on: https://github.com/microsoft/rnx-kit/blob/main/packages/test-app/src/App.native.tsx#L39

@Lexe003
Copy link

Lexe003 commented Feb 25, 2022

It triggers now on all elements inside my React app - also on elements that are not in an array.

@stefcameron
Copy link

With this, I have now 7195 new lint errors for things that are completely not related to an array that never tripped before. For example:

            <Fragment>
              <div data-testid={`${testId}-message`}>{error.message}</div>
              <div data-testid={`${testId}-stack`}>{error.stack}</div>
            </Fragment>

Causes:

  49:15  error  Missing "key" prop for element in array  react/jsx-key
  49:15  error  Missing "key" prop for element in array  react/jsx-key
  50:15  error  Missing "key" prop for element in array  react/jsx-key
  50:15  error  Missing "key" prop for element in array  react/jsx-key

@ljharb
Copy link
Member

ljharb commented Feb 25, 2022

@stefcameron yes, thank you. that's #3215.

@ljharb
Copy link
Member

ljharb commented Feb 25, 2022

@tido64 if you're getting a different problem than #3215, please file a new issue.

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

Successfully merging a pull request may close this issue.

5 participants