Fix useForceUpdate Memory Leak: Only setState if the component is still mounted #30667
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Whilst working with Slot Fills, I found that when removing a component that renders a Slot Fill, a memory leak error is logged to the console:
Tracing it back, it seems to be occurring because
useForceUpdate
returns asetState
method which can still be called after the component consuming it is unmounted. This can be fixed by checking ifuseForceUpdate
is unmounted before callingsetState
.gutenberg/packages/components/src/slot-fill/bubbles-virtually/fill.js
Lines 11 to 14 in 3da717b
This only occurs if using Slot Fills with
bubblesVirtually
enabled.This issue might be related to this note from @diegohaz #17355 (comment), however, in my testing, it only occurred with
bubblesVirtually
on, not off.How has this been tested?
I tested this by applying the fix directly to the node_modules in WooCommerce Gutenberg Products Block plugin, where Slot Fills are imported. After applying the patch I could no longer reproduce the memory leak issue.
Note it can only be tested if:
bubblesVirtually
is onIt was logged here: woocommerce/woocommerce-blocks#4047
Types of changes
This is a bug fix within the
components
package, for Slot Fills.Checklist: