Skip to content

Bug: useDeferredValue gets stuck with a stale value #35821

Closed

Description

@gaearon

Essentially, I see useDeferredValue being stuck and never catching up.

Not sure if it's possible to extract it out of a Next.js app. I think it's probably a React bug because it seems related to core APIs, but I couldn't simplify it past the "render some JSX from server action" repro case.

Demo

Type "hello world".

In dev (npm run dev), the second text area catches up.

In prod (npm run build + npm start), the second text area often gets stuck and never catches up.

bug.mov

Code

Here is a repro case: https://github.com/gaearon/react-udv-bug/

This is the main harness:

export function TestPreviewClient() {
  const [promise, setPromise] = useState<Promise<ReactNode>>(initialPromise);
  const deferred = useDeferredValue(promise);

  function handleChange(value: string) {
    setPromise(renderAction(value));
  }

  return (
    <div style={{ padding: 16, fontFamily: "monospace" }}>
      <textarea
        placeholder="type here"
        onChange={(e) => handleChange(e.target.value)}
        rows={3}
        style={{ width: "100%", fontFamily: "monospace" }}
      />
      <div
        data-testid="deferred"
        style={{ border: "1px solid #ccc", padding: 8, minHeight: 40 }}
      >
        <Suspense fallback={<div>loading...</div>}>
          <Resolved promise={deferred} />
        </Suspense>
      </div>
    </div>
  );
}

Note these helpers:

export function ClientWrapper({ children }: { children: ReactNode }) {
  const t = performance.now();
  while (performance.now() - t < 2) {
    // do nothing
  }
  return <>{children}</>;
}

function Resolved({ promise }: { promise: Promise<ReactNode> }) {
  return <>{use(promise)}</>;
}

And this is the server part:

async function AsyncChild({
  children,
}: {
  children: ReactNode;
}): Promise<ReactNode> {
  await new Promise((r) => setTimeout(r, 1));
  return children;
}

async function Item({ children }: { children: ReactNode }): Promise<ReactNode> {
  return (
    <ClientWrapper>
      <AsyncChild>{children}</AsyncChild>
    </ClientWrapper>
  );
}

export async function renderAction(input: string): Promise<ReactNode> {
  return (
    <Item>
      <div>
        <Item>
          <div>{input}</div>
        </Item>
      </div>
    </Item>
  );
}

You can generally simplify this structure but then it will be harder to reproduce.

Activity

  1. added
    Status: Unconfirmed
    and removed
    Status: Unconfirmed
    on Feb 18, 2026
  2. gaearon commented on Feb 18, 2026

    @gaearon
    CollaboratorAuthor

    If you run into this bug, this workaround helped me:

    function Ticker({ children }: { children: ReactNode }) {
      const [, setTick] = useState(0);
      useEffect(() => {
        const id = setInterval(() => setTick((n) => n + 1), 1000);
        return () => clearInterval(id);
      }, []);
      return <>{children}</>;
    }

    Obviously this re-renders the tree inside every second which might not be amazing. Actually this is probably fine since it should be able to bail out, and this is enough to kick it.

  3. wrspada02 commented on Feb 19, 2026

    @wrspada02
  4. Pantkartik commented on Feb 21, 2026

    @Pantkartik
  5. coder-ishan commented on Feb 24, 2026

    @coder-ishan
  6. gaearon commented on Feb 26, 2026

    @gaearon
    CollaboratorAuthor

    I'm not sure but you could try the repro case I offered and see whether patching this fixes the repro case.

  7. Pantkartik commented on Feb 26, 2026

    @Pantkartik
  8. added 2 commits that reference this issue on Mar 24, 2026
  9. assigned and unassigned on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions