From 8e504f2329ec06354282a61e1fb327664e3508ff Mon Sep 17 00:00:00 2001 From: HuJean <7037477+HuJean@users.noreply.github.com> Date: Tue, 7 Apr 2026 11:01:30 +0800 Subject: [PATCH] refactor: create element without ref for suspense in main thread --- .changeset/legal-zebras-sit.md | 5 +++ packages/react/runtime/src/lynx/suspense.ts | 38 +++++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 .changeset/legal-zebras-sit.md diff --git a/.changeset/legal-zebras-sit.md b/.changeset/legal-zebras-sit.md new file mode 100644 index 0000000000..985a13fc24 --- /dev/null +++ b/.changeset/legal-zebras-sit.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react": patch +--- + +Create element without ref for suspense in main thread. diff --git a/packages/react/runtime/src/lynx/suspense.ts b/packages/react/runtime/src/lynx/suspense.ts index b96abb058a..49cf8ec111 100644 --- a/packages/react/runtime/src/lynx/suspense.ts +++ b/packages/react/runtime/src/lynx/suspense.ts @@ -18,25 +18,33 @@ export const Suspense: FunctionComponent<{ children: VNode | VNode[]; fallback: (__MAIN_THREAD__ ? createElementMainThread : createElementBackground) as typeof createElementBackground; const childrenRef = useRef(); - const newChildren = __createElement('wrapper', { - ref: (bsi: BackgroundSnapshotInstance) => { - if (bsi) { - childrenRef.current = bsi; - } + const newChildren = __createElement( + 'wrapper', + __MAIN_THREAD__ ? {} : { + ref: (bsi: BackgroundSnapshotInstance) => { + if (bsi) { + childrenRef.current = bsi; + } + }, }, - }, children); + children, + ); - const newFallback = __createElement('wrapper', { - ref: (bsi: BackgroundSnapshotInstance) => { - if (bsi && childrenRef.current) { - const i = globalBackgroundSnapshotInstancesToRemove.indexOf(childrenRef.current.__id); - if (i !== -1) { - globalBackgroundSnapshotInstancesToRemove.splice(i, 1); + const newFallback = __createElement( + 'wrapper', + __MAIN_THREAD__ ? {} : { + ref: (bsi: BackgroundSnapshotInstance) => { + if (bsi && childrenRef.current) { + const i = globalBackgroundSnapshotInstancesToRemove.indexOf(childrenRef.current.__id); + if (i !== -1) { + globalBackgroundSnapshotInstancesToRemove.splice(i, 1); + } + childrenRef.current = undefined; } - childrenRef.current = undefined; - } + }, }, - }, fallback); + fallback, + ); return __createElement(PreactSuspense, { fallback: newFallback }, newChildren); };