fix(jsx/dom): should not return memoized result when context is changed (#3792)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent 1e62912 · Browse files · 2 files·+45 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+45 added−1 removed
- tests:+32 added−0 removed
- other:+13 added−1 removed
Provenance
- Commit
fa6f51b- Tree
174131e- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
1e62912
2 files changed+45 added−1 removed
modified:src/jsx/dom/index.test.tsx+32 added
17 unmodified lines18 import DefaultExport, {19 cloneElement,20 cloneElement as cloneElementForDom,21 createElement as createElementForDom,22+ createContext,23+ useContext,24 createPortal,25 flushSync,26 isValidElement,27 memo,1465 unmodified lines1493 await Promise.resolve()1494 expect(root.innerHTML).toBe('<div><div><p>Count: 1</p></div><button>+</button></div>')1495 expect(renderCount).toBe(2)1496 })1497+1498+ it('should not return memoized result when context is not changed', async () => {1499+ const Context = createContext<[number, (arg: number | ((value: number) => number)) => void]>([1500+ 0,1501+ () => {},1502+ ])1503+ const Container: FC<{ children: Child }> = ({ children }) => {1504+ const [count, setCount] = useState(0)1505+ return <Context.Provider value={[count, setCount]}>{children}</Context.Provider>1506+ }1507+ const Content = () => {1508+ const [count, setCount] = useContext(Context)1509+ return (1510+ <>1511+ <span>{count}</span>1512+ <button onClick={() => setCount((c) => c + 1)}>+</button>1513+ </>1514+ )1515+ }1516+ const app = (1517+ <Container>1518+ <Content />1519+ </Container>1520+ )1521+ render(app, root)1522+ expect(root.innerHTML).toBe('<span>0</span><button>+</button>')1523+ root.querySelector('button')?.click()1524+ await Promise.resolve()1525+ expect(root.innerHTML).toBe('<span>1</span><button>+</button>')1526+ })1527 })1528 1529 describe('useRef', async () => {1530 it('simple', async () => {979 unmodified linesmodified:src/jsx/dom/render.ts+13 added-1 removed
449 unmodified lines450 }451 }452 }453 454+const isSameContext = (455+ oldContexts: LocalJSXContexts,456+ newContexts: NonNullable<LocalJSXContexts>457+): boolean =>458+ !!(459+ oldContexts &&460+ oldContexts.length === newContexts.length &&461+ oldContexts.every((ctx, i) => ctx[1] === newContexts[i][1])462+ )463+464 const fallbackUpdateFnArrayMap: WeakMap<465 NodeObject,466 Array<() => Promise<NodeObject | undefined>>467 > = new WeakMap<NodeObject, Array<() => Promise<NodeObject | undefined>>>()66 unmodified lines534 const pP = (oldChild.pP = oldChild.props)535 oldChild.props = child.props536 oldChild.f ||= child.f || node.f537 if (typeof child.tag === 'function') {538+ const oldContexts = oldChild[DOM_STASH][2]539 oldChild[DOM_STASH][2] = child[DOM_STASH][2] || []540 oldChild[DOM_STASH][3] = child[DOM_STASH][3]541 542 if (543 !oldChild.f &&544 ((oldChild.o || oldChild) === child.o || // The code generated by the react compiler is memoized under this condition.534- (oldChild.tag as MemorableFC<unknown>)[DOM_MEMO]?.(pP, oldChild.props)) // The `memo` function is memoized under this condition.545+ (oldChild.tag as MemorableFC<unknown>)[DOM_MEMO]?.(pP, oldChild.props)) && // The `memo` function is memoized under this condition.546+ isSameContext(oldContexts, oldChild[DOM_STASH][2])547 ) {548 oldChild.s = true549 }550 }244 unmodified lines