Skip to content

Commit 63c4074

Browse files
committed
fix: csr should load data in useEffect
1 parent 1c6cfcf commit 63c4074

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

packages/bridge/bridge-react/src/lazy/createLazyComponent.tsx

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { ReactNode } from 'react';
1+
import React, { ReactNode, useState, useEffect } from 'react';
22
import logger from './logger';
3-
import { AwaitDataFetch } from './AwaitDataFetch';
3+
import { AwaitDataFetch, transformError } from './AwaitDataFetch';
44
import {
55
fetchData,
66
getDataFetchItem,
@@ -186,7 +186,7 @@ function getServerNeedRemoteInfo(
186186
globalName: loadedRemoteInfo.entryGlobalName,
187187
};
188188
}
189-
return;
189+
return undefined;
190190
}
191191

192192
export function createLazyComponent<T, E extends keyof T>(
@@ -241,7 +241,7 @@ export function createLazyComponent<T, E extends keyof T>(
241241
id: moduleId,
242242
remoteSnapshot: loadedRemoteInfo.snapshot,
243243
}),
244-
{ name: instance!.name, version: instance?.options.version },
244+
{ name: instance.name, version: instance.options.version },
245245
);
246246
logger.debug('getData dataFetchMapKey: ', dataFetchMapKey);
247247
if (!dataFetchMapKey) {
@@ -319,17 +319,67 @@ export function createLazyComponent<T, E extends keyof T>(
319319
});
320320

321321
return (props: ComponentType) => {
322+
// eslint-disable-next-line max-lines
322323
const { key, ...args } = props;
324+
if (globalThis.FEDERATION_SSR && !options.noSSR) {
325+
return (
326+
<AwaitDataFetch
327+
resolve={getData(options.noSSR)}
328+
loading={options.loading}
329+
errorElement={options.fallback}
330+
>
331+
{/* @ts-expect-error ignore */}
332+
{(data) => <LazyComponent {...args} mfData={data} />}
333+
</AwaitDataFetch>
334+
);
335+
} else {
336+
// Client-side rendering logic
337+
const [data, setData] = useState<unknown>(null);
338+
const [loading, setLoading] = useState<boolean>(true);
339+
const [error, setError] = useState<ErrorInfo | null>(null);
323340

324-
return (
325-
<AwaitDataFetch
326-
resolve={getData(options.noSSR)}
327-
loading={options.loading}
328-
errorElement={options.fallback}
329-
>
330-
{/* @ts-ignore */}
331-
{(data) => <LazyComponent {...args} mfData={data} />}
332-
</AwaitDataFetch>
333-
);
341+
useEffect(() => {
342+
let isMounted = true;
343+
const fetchDataAsync = async () => {
344+
try {
345+
setLoading(true);
346+
const result = await getData(options.noSSR);
347+
if (isMounted) {
348+
setData(result);
349+
}
350+
} catch (e) {
351+
if (isMounted) {
352+
setError(transformError(e as Error));
353+
}
354+
} finally {
355+
if (isMounted) {
356+
setLoading(false);
357+
}
358+
}
359+
};
360+
361+
fetchDataAsync();
362+
363+
return () => {
364+
isMounted = false;
365+
};
366+
}, []);
367+
368+
if (loading) {
369+
return <>{options.loading}</>;
370+
}
371+
372+
if (error) {
373+
return (
374+
<>
375+
{typeof options.fallback === 'function'
376+
? options.fallback(error)
377+
: options.fallback}
378+
</>
379+
);
380+
}
381+
// @ts-expect-error ignore
382+
return <LazyComponent {...args} mfData={data} />;
383+
}
334384
};
335385
}

0 commit comments

Comments
 (0)