Skip to content

Forward ref's don't support the react 19 cleanup function #8024

@lukpsaxo

Description

@lukpsaxo

Provide a general summary of the issue here

See https://react.dev/blog/2024/12/05/react-19#cleanup-functions-for-refs

and:

export function useObjectRef<T>(forwardedRef?: ((instance: T | null) => void) | MutableRefObject<T | null> | null): MutableRefObject<T | null> {
const objRef: MutableRefObject<T | null> = useRef<T>(null);
return useMemo(() => ({
get current() {
return objRef.current;
},
set current(value) {
objRef.current = value;
if (typeof forwardedRef === 'function') {
forwardedRef(value);
} else if (forwardedRef) {
forwardedRef.current = value;
}
}
}), [forwardedRef]);

🤔 Expected Behavior?

if a ref is provided to a component and I return a function for cleanup from it, it is called on cleanup.

😯 Current Behavior

the cleanup function is not respected.

💁 Possible Solution

Keep a reference to the cleanup function and call it as required - keeping the react 19 behavior of not calling with null if a callback is returned.

I will be happy to make a PR.

🔦 Context

we are doing complex handling in ref callbacks around the table and we can't use this new react 19 functionality.

Note also that it will in future become the default and they will stop sending null.

🖥️ Steps to Reproduce

use a ref on a component using useObjectRef

Version

3.38.1

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Windows

🧢 Your Company/Team

Saxo Bank

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions