Skip to content

Resolving deprecation of findDOMNode in StrictMode #693

Closed

Description

@mustaphaboudouch

Problem

Getting this warning when using the deprecated function findDOMNode in StrictMode.
findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of DraggableCore which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here

Suggested Solution

To resolve this warning, I suggest replacing findDOMNode with an innerRef. If the user passes a ref as a prop, we'll use it as the draggable ref. Otherwise, we'll create an internal ref and use it as the draggable ref.
We can then easily access the draggable DOM element : ref.current

Here is the implementation :

class DraggableCore extends React.Component<DraggableCoreProps, DraggableCoreState> {
	element: any =
		typeof this.props.innerRef === 'function'
			? { current: null }
			: React.createRef<HTMLElement>();

	// Now w'll use `getElement` instead of `findDOMNode`
	getElement = () =>
		(this.props.innerRef && typeof this.props.innerRef !== 'function'
			? this.props.innerRef
			: this.element
		).current;

	// Now if we need draggable DOM element :
	// const element = this.getElement();

	render() {
		const { innerRef, children, ...props } = this.props;

		return React.cloneElement(React.Children.only(children), {
			ref: typeof innerRef === 'function'
				? (current: HTMLElement) => {
					innerRef(current);
					this.element.current = current;
				}
				: innerRef || this.element,
			...props,
		});
	}
}

And here is a simple usage :

const myExternalRef = useRef(null)

// `innerRef` is optional. If it's not passed, DraggableCore will create an internal ref
<DraggableCore innerRef={myExternalRef}>
	<span>Test</span>
</DraggableCore>

Activity

  1. carlbrn commented on Apr 9, 2023

    @carlbrn
  2. STRML commented on Sep 27, 2023

    @STRML
    Collaborator

    Closing, see nodeRef in README.

  3. rhyek commented on Apr 27, 2025

    @rhyek

    This does not work when actually using React 19. More info here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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