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

Aperta
#173 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
react, typescript
Ambito
frontend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
TypeScript
Stelle
581
Fork
28
Merge medio
13h 48m
PR unite (30g)
45

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/redact

Tutte le issue di TanStack/redact

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.