Skip to content

Bug: Nested lazy components cause rerendering #29235

Closed
@raymondwang

Description

@raymondwang

This is a duplicate of #27573, which was closed without response. We're noticing this issue at scale, and it's fairly pronounced, with hundreds of rapid re-renders triggered by a single lazy component. I've forked the replication from original issue to demonstrate that this is still an issue in React 19:

https://codesandbox.io/p/sandbox/react-lazy-rerender-bug-forked-q4q6ym

The current behavior

The lazy parent component renders multiple times:

Screenshot 2024-05-23 at 10 54 06 AM

(In our environment, we've seen the number of re-renders proliferate to the hundreds. This matches the below reproduction.)

The expected behavior

The lazy parent component should only render one time.


The original issue had a reply that included a reproduction that matched the issue we're experiencing more closely: https://codesandbox.io/p/sandbox/react-lazy-suspense-5g75x3

Similar to what we're seeing, this repro has hundreds of re-renders, and the number is nondeterministic.

Screenshot 2024-05-23 at 11 06 11 AM Screenshot 2024-05-23 at 11 06 21 AM

Our workaround for now is a lightweight replacement for Suspense using the Transition API to defer its lazy children, for usage in nested lazy contexts:

export const DeferredSuspense: FunctionComponent<SuspenseProps> = (props) => {
  const [isDeferred, setIsDeferred] = useState(true);

  useEffect(() => {
    startTransition(() => setIsDeferred(false));
  }, []);

  if (isDeferred) {
    return props.fallback;
  }

  return <Suspense {...props} />;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivityStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions