-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
GradientPicker: use index while iterating over gradient entries to avoid React "duplicated key" warning #57361
Conversation
…oid React "duplicated key" warning
@@ -46,7 +46,7 @@ function SingleOrigin( { | |||
const gradientOptions = useMemo( () => { | |||
return gradients.map( ( { gradient, name }, index ) => ( | |||
<CircularOptionPicker.Option | |||
key={ gradient } | |||
key={ index } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we can't use the slug
? It's marked as required in TS and the JSON schema says it should be a unique identifier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the custom color palette (PaletteEdit
component), this slug is automatically generated from the name entered by the user. Therefore, if a user defines the same color name across multiple palettes, duplicate slugs will occur.
Also, as reported in #55185, the numbers do not increase in non-English languages, so slug duplication will occur.
However, this is unexpected behavior and I think it should be improved in the future so that duplication of slugs does not occur in any scenario, so I updated this PR to use the slug as a key as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, thanks for the explanation. I agree the slug collision problem should be handled before the data hits the GradientPicker component 👍
Flaky tests detected in 2eff803. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7329196636
|
Part of #57309
Similar to #43096
What?
This PR avoids the React Warning error that occurs when the
GradientPicker
component renders entries with duplicate styles.b6ca74abb97903934b2750c8cc1c1e81.mp4
Why?
Now the actual gradient style is used as the key. However, this style is user-definable via theme.json or a custom palette, so duplication can occur.
How?
Use
index
for thekey
prop. I've heard that using index as a key is not recommended, but I think we have to use it in this case.The reason this error does not occur with solid color is that index is used for the key.
Testing Instructions
Please update Emptytheme's theme.json as follows in advance. The first and second in the gradient palette have the same style.
test/emptytheme/theme.json
Note: The first color is transparent, but this is an issue that needs to be fixed separately as reported in this comment.