From 48abecf2e5a58c79fd9d71fc8028eb7072d4eba1 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Wed, 3 Aug 2022 12:54:12 +0200 Subject: [PATCH] Unmount react when WpBlock is disconnected --- src/gutenberg-packages/frontend.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/gutenberg-packages/frontend.js b/src/gutenberg-packages/frontend.js index c2ce4629..885b4fb9 100644 --- a/src/gutenberg-packages/frontend.js +++ b/src/gutenberg-packages/frontend.js @@ -1,6 +1,7 @@ import { Consumer, createProvider } from './react-context'; import { createGlobal, matcherFromSource } from './utils'; import { EnvContext, hydrate } from './wordpress-element'; +import { unmountComponentAtNode } from 'react-dom'; const blockTypes = createGlobal('wpBlockTypes', new Map()); @@ -179,6 +180,11 @@ class WpBlock extends HTMLElement { } }); } + + disconnectedCallback() { + // Unmount the React component, running callbacks and cleaning up its state. + unmountComponentAtNode(this); + } } // We need to wrap the element registration code in a conditional for the same