useDeferredValue with an object created during render hangs the page after any re-render (0.3.0)

未关闭
#153 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
57/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript, vite
领域
frontend

调研方向

Start by tracing v() in packages/redact/src/dom/transitions.ts, then follow how enqueue, scheduleUpdate, flushRoot, renderUpdates, and flushTransitions handle the fiber. Reproduce with the supplied React component and Vite build, with and without redact(). Done when the page stays responsive after the post-mount re-render and a regression check covers this case.

由索引模型根据 Issue 内容生成。

描述

Summary

Passing an object created during render to useDeferredValue hangs the tab as soon as the component re-renders after mount. React settles after 3 renders. Redact renders forever and keeps throwing from the flushPending guard, so the main thread stays busy and the page stops responding.

React's docs warn that this pattern causes an unnecessary background render. It is wasteful there, but it finishes.

Reproduction

No router, Suspense, compiler or hydration is involved.

import { useDeferredValue, useEffect, useState } from 'react'
import { createRoot } from 'react-dom/client'

window.renders = 0

function App() {
  window.renders++
  const [, setTick] = useState(0)
  useDeferredValue({})
  useEffect(() => setTick(1), [])
  return <p>done</p>
}

createRoot(document.getElementById('root')).render(<App />)

Build with vite build with and without the redact() plugin, then open the page.

Runtime Renders after about 3 s Page errors Page
React 19.3.1 3 0 Responsive
Redact 0.3.0 3,873,811 44,178 Unresponsive

These are unminified production builds, so the errors are the bare Error() from flushPending. The count is what had accumulated when I paused the page in the debugger.

The first render does not loop. It takes any re-render after mount, and in my tests a setState in an effect, a setState in a layout effect and a useSyncExternalStore update all triggered it.

Possible cause

This comes from reading the source. I have not tested a patch.

In v() in packages/redact/src/dom/transitions.ts, a normal render whose value differs from hook.s calls enqueue(fiber), which also calls scheduleUpdate(fiber). flushRoot then runs renderUpdates followed by flushTransitions. The transition pass accepts the value, but the fiber still seems to be queued for a normal pass. That pass creates another new object, which differs from hook.s, so it enqueues again.

Real-world impact

I hit this in a TanStack Start app where a hook returned a new query options object on every render, with a stable query key, and the result went to useDeferredValue. The page froze on load under Redact and works under React.

Memoizing the value avoids the loop.

Environment
  • @tanstack/redact 0.3.0 (also 0.2.5), default redact() preset
  • React 19.3.1 (control)
  • Vite 7.3.7, @vitejs/plugin-react 5.2.0
  • Chromium 156 (Playwright), Node 26
主要语言
TypeScript
星标
581
派生
28
平均合并
13 小时 48 分钟
30 天内合并 PR
45

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。