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(chart): add index fallback for chart legend key #6361

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sanghyoLe
Copy link

- Add index as fallback when item.value is undefined in radial charts
- Fixes shadcn-ui#6243
Copy link

vercel bot commented Jan 15, 2025

@sanghyoLe is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

const key = `${nameKey || item.dataKey || "value"}`
const itemConfig = getPayloadConfigFromPayload(config, item, key)

return (
<div
key={item.value}
key={item.value || index}
Copy link

@youneshenniwrites youneshenniwrites Jan 15, 2025

Choose a reason for hiding this comment

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

Using the index as a key can lead to re-render issues and state loss (as stated by the React docs here).

Instead, consider combining the index with another unique identifier:

key={`${index}-${Math.random().toString(36).substr(2, 9)}`}

Or preferably, use item.dataKey as in lin 289.

Copy link
Author

Choose a reason for hiding this comment

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

Some chart items don't have dataKey available, so we can't use it as a unique identifier.
I'll check if there are other consistent unique identifiers we could use instead and update accordingly.

Copy link
Author

Choose a reason for hiding this comment

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

I found that for some chart types like RadialChart, the value is passed through item.payload.value instead of item.value. I've updated the code to use both item.value || item.payload?.value as the key to handle these cases, providing a more reliable unique identifier than the index.

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.

[bug]: Chart Legend Each child in a list should have a unique "key" prop.
2 participants