Skip to content

react/jsx-key should violate when key is potentially undefined #3920

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

Open
jroru opened this issue Apr 29, 2025 · 1 comment
Open

react/jsx-key should violate when key is potentially undefined #3920

jroru opened this issue Apr 29, 2025 · 1 comment

Comments

@jroru
Copy link

jroru commented Apr 29, 2025

interface Dog {
  id?: string;
  name: string;
}

const dogs: Dog[] = [
  { id: "spikey", name: "spike" },
  { name: "spot" },
] as const;

export const Dogs = () => (
  <div>
    {dogs.map((dog) => (
      <div key={dog.id}>{dog.name}</div>
    ))}
  </div>
);

The above code does not currently violate jsx-key. With type information it could, and I believe it really should. Something that can potentially be undefined is unsuitable as a key.

@ljharb
Copy link
Member

ljharb commented Apr 30, 2025

That's only true if more than one of the items has an undefined id.

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

No branches or pull requests

2 participants