motion icon indicating copy to clipboard operation
motion copied to clipboard

[BUG] React 19 ref cleanup is not respected

Open Malien opened this issue 5 months ago • 1 comments

React 19 added ref cleanup functions, and deprecated the current "call with null on unmount" behaviour.

https://codesandbox.io/p/devbox/66jh5j

<div
  ref={(instance: HTMLDivElement) => {
    doAction(instance)
    return () => cleanup()
  })
/>

// mount: const cleanup = ref(div)
// unmount: clenaup()

vs

<div
  ref={(instance: HTMLDivElement | null) => {
    if (instance) doAction(instance)
    else cleanup()
  })
/>

// mount: ref(div)
// unmount: ref(null)

When the ref callback returns a cleanup function, the callback won't be called again with the null value. Instead the cleanup function will be called.

Motion components (motion.div) don't respect that convention and will always do the null thing.

<motion.div
  ref={(instance: HTMLDivElement) => {
    doAction(instance)
    return () => cleanup()
  })
/>

// mount: ref(div)
// unmount: ref(null)

Malien avatar Sep 05 '25 09:09 Malien

Hi @Malien ,

I’ve started looking into this issue and would like to work on a fix. Could you please assign this issue to me?

AmitSingh-5600 avatar Sep 23 '25 18:09 AmitSingh-5600