Skip to content

Commit 9804fcd

Browse files
committed
fix: add hack to fix react unmount issue
1 parent 0efb6f7 commit 9804fcd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/plugin.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"author": "LinOnetwo",
55
"core-version": ">=5.1.22",
66
"plugin-type": "plugin",
7-
"version": "0.3.10",
7+
"version": "0.4.0",
88
"list": "readme"
99
}

src/widget.ts

+26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ const Widget = require('$:/core/modules/widgets/widget.js').widget as typeof IWi
99
const ReactDom: ReactDomType = require('react-dom');
1010
const React: ReactType = require('react');
1111

12+
// TODO: remove this hack after https://github.com/Jermolene/TiddlyWiki5/pull/6699 merged
13+
/*
14+
Remove any DOM nodes created by this widget or its children
15+
*/
16+
Widget.prototype.removeChildDomNodes = function(parentRemoved) {
17+
// If this widget has directly created DOM nodes, delete them and exit. This assumes that any child widgets are contained within the created DOM nodes, which would normally be the case
18+
// If parent has already detatch its dom node from the document, we don't need to do it again.
19+
if(this.domNodes.length > 0 && !parentRemoved) {
20+
$tw.utils.each(this.domNodes,function(domNode) {
21+
domNode.parentNode.removeChild(domNode);
22+
});
23+
this.domNodes = [];
24+
// inform child widget to do some custom cleanup in a overrided sub-class method, and tell child widget that parent has already done the update, so children don't need to do anything.
25+
parentRemoved = true;
26+
}
27+
// If parentRemoved is unset or false, will ask the child widgets to delete their DOM nodes
28+
$tw.utils.each(this.children,function(childWidget) {
29+
childWidget.removeChildDomNodes(parentRemoved);
30+
});
31+
};
32+
1233
export interface IDefaultWidgetProps {
1334
parentWidget?: IWidget;
1435
}
@@ -65,6 +86,11 @@ export class ReactWidget<
6586
const reactElement = React.createElement(this.reactComponent, currentProps);
6687
this.root.render(reactElement);
6788
}
89+
90+
removeChildDomNodes() {
91+
super.removeChildDomNodes();
92+
this.root?.unmount?.();
93+
}
6894
}
6995

7096
exports.widget = ReactWidget;

0 commit comments

Comments
 (0)