Skip to content

Commit b302e84

Browse files
committed
Add guard for null refs in setButtonStyles callback
1 parent 8457627 commit b302e84

File tree

1 file changed

+11
-1
lines changed
  • packages/block-library/src/image

1 file changed

+11
-1
lines changed

packages/block-library/src/image/view.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,19 @@ const { state, actions, callbacks } = store(
356356
`;
357357
},
358358
setButtonStyles() {
359-
const { imageId } = getContext();
360359
const { ref } = getElement();
361360

361+
// This guard prevents errors in images with the `srcset`
362+
// attribute, which can dispatch `load` events even after DOM
363+
// removal. Preact doesn't automatically clean up `load` event
364+
// listeners on unmounted `img` elements (see
365+
// https://github.com/preactjs/preact/issues/3141).
366+
if ( ! ref ) {
367+
return;
368+
}
369+
370+
const { imageId } = getContext();
371+
362372
state.metadata[ imageId ].imageRef = ref;
363373
state.metadata[ imageId ].currentSrc = ref.currentSrc;
364374

0 commit comments

Comments
 (0)