-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
List View: Fix warning error when the gallery block has the same image URLs #53809
Conversation
bb73960
to
a0387b7
Compare
Size Change: +13 B (0%) Total Size: 1.51 MB
ℹ️ View Unchanged
|
@@ -194,7 +194,7 @@ function ListViewBlockSelectButton( | |||
{ images.map( ( image, index ) => ( | |||
<span | |||
className="block-editor-list-view-block-select-button__image" | |||
key={ `img-${ image.url }` } | |||
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.
Using the index
as a key
isn't recommended. Maybe we could use clientId
here.
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.
Thanks for the review!
Using the
index
as akey
isn't recommended.
I did not know about this 😅 I have taken the following actions:
- I have renamed the
getImageUrl
function to thegetImage
function. This is because this function returns an object containing information about the image. - Include the
clientId
in the return value of thegetImage
function and use it as a key for the map function.
Flaky tests detected in 4863836. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5903964340
|
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.
Thanks for fixing this @t-hamano! Using the clientId
sounds like a good way to keep the key unique for now 👍
In a follow-up, we'll likely be looking at creating some kind of API for exposing image urls from the block to the list view (i.e. to implement #53684). In that case, we might not want blocks to have to pass in a clientId
(ideally a block would just return an image url) so we may need to change the logic slightly when we get to that. One possible approach could be to create a composite key using the URL + index, so that if either changes, a re-render will be triggered. But that's not a blocker for now, I think the requirements will become clearer once there's a WIP PR to play around with the API 🙂
In testing trunk, I can see the existing key
fails for duplicate url keys, resulting in unintended duplicate rendering, most notable when moving child blocks. With this PR applied, with the Gallery block collapsed and then rearranging child images, the UI updates correctly and there are never more than 3 image thumbnails:
Before | After |
---|---|
LGTM! ✨
Thanks for the review, @andrewserong! Thanks for the information about the new API as well 🙇 |
Related to #53728
What?
This PR fixes an error output to the browser console when opening a list view if the gallery block has images with the same URL.
Why?
This is because
key
props are duplicated in themap()
function.How?
Use
index
. This change should have no impact on functionality, but if there is a problem, please let me know.Testing Instructions