Skip to content

Commit 5acef43

Browse files
author
Kevin Logan
committed
Revert "Avoid warning by calling createRoot in main.ts"
This reverts commit 8d39025.
1 parent 8d39025 commit 5acef43

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

Diff for: src/app/header/header-wrapper.component.tsx

+26-31
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@angular/core";
1414
import * as React from "react";
1515
import * as ReactDOM from "react-dom";
16-
import { Root, createRoot } from "react-dom/client";
16+
import { createRoot } from "react-dom/client";
1717
import { Header } from "./Header";
1818
const containerElementName = "reactHeaderComponentContainer";
1919

@@ -35,43 +35,38 @@ export class HeaderWrapperComponent
3535

3636
constructor() {}
3737
ngOnChanges(changes: SimpleChanges): void {
38-
// note: It works, but I get this warning from changed events (from the @Input update approach)
39-
// Warning: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it.
40-
// I've decided to ignore it since the render is efficient
41-
// If you call render on the same root more than once, React will update the DOM as necessary to reflect the latest JSX you passed. React will decide which parts of the DOM can be reused and which need to be recreated by “matching it up” with the previously rendered tree. Calling render on the same root again is similar to calling the set function on the root component: React avoids unnecessary DOM updates.
42-
// see https://react.dev/learn/preserving-and-resetting-state
43-
// these can be avoided by using the document.addEventListener approach
44-
// it is also avoided with calling createRoot in the main.ts file
45-
console.log("changed");
46-
HeaderWrapperComponent.render(undefined);
38+
console.log('changed');
39+
this.render();
4740
}
48-
41+
4942
ngAfterViewInit() {
50-
console.log("afterViewInit");
51-
HeaderWrapperComponent.render(undefined);
43+
console.log('afterViewInit');
44+
this.render();
5245
}
5346

5447
ngOnDestroy() {
5548
ReactDOM.unmountComponentAtNode(this.containerRef.nativeElement);
5649
}
5750

58-
static reactHeaderRoot: Root;
59-
public static render(reactHeaderRoot: Root | undefined) {
60-
// set in main.ts
61-
if (reactHeaderRoot) HeaderWrapperComponent.reactHeaderRoot = reactHeaderRoot;
62-
if (HeaderWrapperComponent.reactHeaderRoot)
63-
HeaderWrapperComponent.reactHeaderRoot.render(
64-
<React.StrictMode>
65-
<Header
66-
{...{
67-
headerComponents: [
68-
// without the ts-ignore, this won't compile
69-
/* @ts-ignore */
70-
<notification-web-component counter={this.appNotificationCounter}></notification-web-component>,
71-
],
72-
}}
73-
/>
74-
</React.StrictMode>
75-
);
51+
private render() {
52+
// note: I do get this warning from ngAfterViewInit and changed events
53+
// Warning: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it.
54+
// I've decided to ignore it since the render is efficient
55+
// If you call render on the same root more than once, React will update the DOM as necessary to reflect the latest JSX you passed. React will decide which parts of the DOM can be reused and which need to be recreated by “matching it up” with the previously rendered tree. Calling render on the same root again is similar to calling the set function on the root component: React avoids unnecessary DOM updates.
56+
// see https://react.dev/learn/preserving-and-resetting-state
57+
const root = createRoot(this.containerRef.nativeElement as HTMLElement);
58+
root.render(
59+
<React.StrictMode>
60+
<Header
61+
{...{
62+
headerComponents: [
63+
// without the ts-ignore, this won't compile
64+
/* @ts-ignore */
65+
<notification-web-component counter={this.appNotificationCounter}></notification-web-component>,
66+
],
67+
}}
68+
/>
69+
</React.StrictMode>
70+
);
7671
}
7772
}

Diff for: src/main.ts

-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
22

33
import { AppModule } from './app/app.module';
4-
import { createRoot } from 'react-dom/client';
5-
import { HeaderWrapperComponent } from './app/header/header-wrapper.component';
64

75

86
platformBrowserDynamic().bootstrapModule(AppModule)
97
.catch(err => console.error(err));
10-
11-
// this is a way to avoid the
12-
// Warning: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it.
13-
// from @input changes
14-
// poll dom until this element is available
15-
function pollForHeader() {
16-
if (document.querySelector('app-header-wrapper')) {
17-
const header = document.querySelector('app-header-wrapper') as Element;
18-
const root = createRoot(header); 
19-
HeaderWrapperComponent.render(root);
20-
} else {
21-
setTimeout(pollForHeader, 100);
22-
}
23-
}
24-
pollForHeader();

0 commit comments

Comments
 (0)