Skip to content
Myr

fix(jsx/dom): execute previous ref cleanup when ref prop changes on re-render (#5264)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx/dom): execute previous ref cleanup when ref prop changes on re-render * refactor(jsx/dom): simplify ref cleanup condition * refactor(jsx/dom): simplify ref cleanup check in applyProps * test(jsx/dom): add test for ref cleanup on ref prop removal ---------

authored by ADITYA KUMAR JHA · committed by GitHub · parent 7967760 · Browse files · 2 files+81 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+81 added−0 removed

  • tests:+79 added−0 removed
  • other:+2 added−0 removed

2 files+81 added−0 removed

modified:src/jsx/dom/index.test.tsx+79 added
344 unmodified lines
345 expect(root.innerHTML).toBe('<button>remove</button>')346 expect(ref).toBeCalledTimes(1)347 expect(cleanup).toBeCalledTimes(1)348 })349350 it('ref cleanup on ref prop change', async () => {351 const cleanup1 = vi.fn()352 const ref1 = vi.fn().mockReturnValue(cleanup1)353 const cleanup2 = vi.fn()354 const ref2 = vi.fn().mockReturnValue(cleanup2)355356 const App = () => {357 const [currentRef, setCurrentRef] = useState(() => ref1)358 return (359 <>360 <div ref={currentRef} />361 <button onClick={() => setCurrentRef(() => ref2)}>switch</button>362 </>363 )364 }365 render(<App />, root)366 expect(ref1).toHaveBeenCalledTimes(1)367 expect(ref1).toHaveBeenLastCalledWith(expect.any(dom.window.HTMLDivElement))368 expect(cleanup1).toHaveBeenCalledTimes(0)369 expect(ref2).toHaveBeenCalledTimes(0)370371 root.querySelector('button')?.click()372 await Promise.resolve()373374 expect(cleanup1).toHaveBeenCalledTimes(1)375 expect(ref2).toHaveBeenCalledTimes(1)376 expect(ref2).toHaveBeenLastCalledWith(expect.any(dom.window.HTMLDivElement))377 expect(cleanup2).toHaveBeenCalledTimes(0)378 })379380 it('does not call ref cleanup when ref reference does not change on re-render', async () => {381 const cleanup = vi.fn()382 const ref = vi.fn().mockReturnValue(cleanup)383384 const App = () => {385 const [count, setCount] = useState(0)386 return (387 <>388 <div ref={ref} />389 <button onClick={() => setCount(count + 1)}>rerender</button>390 </>391 )392 }393394 render(<App />, root)395 expect(ref).toHaveBeenCalledTimes(1)396 expect(cleanup).toHaveBeenCalledTimes(0)397398 root.querySelector('button')?.click()399 await Promise.resolve()400401 expect(ref).toHaveBeenCalledTimes(1)402 expect(cleanup).toHaveBeenCalledTimes(0)403 })404405 it('ref cleanup on ref prop removal', async () => {406 const cleanup = vi.fn()407 const ref = vi.fn().mockReturnValue(cleanup)408409 const App = () => {410 const [hasRef, setHasRef] = useState(true)411 return (412 <>413 <div {...(hasRef ? { ref } : {})} />414 <button onClick={() => setHasRef(false)}>remove</button>415 </>416 )417 }418419 render(<App />, root)420 expect(ref).toHaveBeenCalledTimes(1)421 expect(cleanup).toHaveBeenCalledTimes(0)422423 root.querySelector('button')?.click()424 await Promise.resolve()425426 expect(cleanup).toHaveBeenCalledTimes(1)427 })428 })429430 describe('child component', () => {431 it('simple', async () => {
2596 unmodified lines
modified:src/jsx/dom/render.ts+2 added
185 unmodified lines
186 }187 } else if (key === 'dangerouslySetInnerHTML' && value) {188 container.innerHTML = value.__html189 } else if (key === 'ref') {190 refCleanupMap.get(container)?.()191 let cleanup192 if (typeof value === 'function') {193 cleanup = value(container) || (() => value(null))194 } else if (value && 'current' in value) {
62 unmodified lines
257 if (eventSpec) {258 container.removeEventListener(eventSpec[0], value, eventSpec[1])259 } else if (key === 'ref') {260 refCleanupMap.get(container)?.()261 refCleanupMap.delete(container)262 } else {263 try {264 container.removeAttribute(toAttributeName(container, key))265 } catch (e) {
562 unmodified lines