NOTE: this happens in both dev and production, and I'm not using <StrictMode> at all.
It appears to work fine:
B.js
export default function B()
{
return <p>B</p>
}
TestApp.js
import { lazy, Suspense } from "react";
const B = lazy(()=>import("./B"));
export default function TestApp()
{
const counter = useRef(0);
counter.current++;
console.log("rendering TestApp - counter",counter.current);
return <Suspense fallback={<p>loading B...</p>}><B/></Suspense>
}
index.js
import { lazy, Suspense } from "react";
import {createRoot} from "react-dom/client";
const TestApp = lazy(()=>import("./TestApp"));
const root = createRoot(document.getElementById("root"));
root.render(<Suspense fallback={<p>loading testapp...</p>}><TestApp/></Suspense>);
but when there is an additional <Suspense> deeper in the component tree (in TestApp itself, suspending <B>), <TestApp> gets duplicated.
project: https://github.com/jmlee2k/react-suspense-root
demo: https://jmlee2k.github.io/react-suspense-root/ (production build)
To see the issue, go to the demo and open the console, you'll see "rendering TestApp - counter 1" twice. If this was simply a double-render, I would expect the counter to increase.
I'm fairly new to react and am very aware I could be doing something wrong, but any info would be appreciated.
Thanks in advance!