Skip to content

Commit

Permalink
SWC-6882 - Asynchronously render as well in ReactComponentV2
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgros committed Aug 30, 2024
1 parent 53e8172 commit 3b85d79
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}

private void synchronizeReactDomRoot() {
if (isRenderedAsReactComponentChild()) {
// This component is rendered as a child of another React component, so destroy the root if one exists
destroyRoot();
} else {
createRoot();
}
}

private ReactElement<?, ?>[] getChildReactElements() {
if (this.allChildrenAreReactComponents()) {
// If all widget children are ReactNodes, get their ReactElements and add them as React children
Expand All @@ -195,13 +186,25 @@ private void synchronizeReactDomRoot() {
}

public void render() {
synchronizeReactDomRoot();
if (isRenderedAsReactComponentChild()) {
// This component will be rendered as a child of another React component, so destroy the root if one exists
destroyRoot();
}

// This component may be a React child of another component, so retrieve the root widget that renders this component tree.
ReactComponentV2<?, ?> componentToRender = getRootReactComponentWidget();
componentToRender.synchronizeReactDomRoot();
// Create a fresh ReactElement tree and render it
componentToRender.root.render(componentToRender.createReactElement());

// Asynchronously schedule creating a root in case we queued up an unmount of the current root
// See https://stackoverflow.com/questions/73459382
Timer t = new Timer() {
@Override
public void run() {
componentToRender.createRoot();
// Create a fresh ReactElement tree and render it
componentToRender.root.render(componentToRender.createReactElement());
}
};
t.schedule(0);
}

@Override
Expand Down

0 comments on commit 3b85d79

Please sign in to comment.