`lazy(memo(Component))` doesn't re-render on store update when an ancestor reads the same store

オープン
#173 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
1/5
見積もり時間
1時間未満
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
react, typescript
領域
frontend

調査の方向性

The issue names renderNode, renderMemo, and the regression test tests/lazy-memo-store-update.test.tsx. Read the linked fix commit and that test first to understand the change already made. The reported behavior is reproduced when the ancestor and lazy memo component subscribe to the same store; the issue is already addressed by the linked commit.

索引モデルが issue の本文から書いたものです。

説明

In our dashboard, a navbar drawer is loaded with lazy(() => import(...)) and wrapped in memo. It reads a value from an external store with useSyncExternalStore. The layout route reads the same store to show the panel title.

When the panel opened, the layout updated (the title switched), but the drawer never re-rendered when redact is enabled. It kept isOpen === false, so the panel stayed empty. No error, nothing in the console. It works with React, and it works as soon as you drop either lazy or memo.

Repro
const store = createStore(false) // minimal subscribe/getSnapshot store

const Panel = lazy(async () => ({
  default: memo(() => {
    const isOpen = useSyncExternalStore(store.subscribe, store.get)
    return <span id="child">{String(isOpen)}</span>
  }),
}))

const App = () => {
  const isOpen = useSyncExternalStore(store.subscribe, store.get)
  return (
    <div title={String(isOpen)}>
      <Suspense fallback="Loading"><Panel /></Suspense>
    </div>
  )
}

// after mount:
store.set(true)
// parent title -> "true"
// #child       -> still "false"

Cause

renderNode clears fiber.g for every tag except Memo, so memo can still see a pending update. A lazy fiber delegates to its resolved memo on the same fiber, but Lazy wasn't exempt. By the time renderMemo runs the flag is gone, so it bails on equal props. Then the component's own queued update gets skipped because g is already false. That only happens when the ancestor renders first in the same pass, which is why both subscribing to the same store matters.

Fix

Keep the flag on lazy fibers like Memo does, and clear it when the lazy suspends. Regression test included: tests/lazy-memo-store-update.test.tsx.

Fix: https://github.com/aymericzip/redact/commit/0b0e5f6e4428b5d79956cf84be1e8c99f8139ea1

主要言語
TypeScript
スター
581
フォーク
28
平均マージ
13時間 48分
マージ済み PR(30日)
45

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。