Skip to content

Resolving deprecation of findDOMNode in StrictMode #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mustaphaboudouch opened this issue Dec 28, 2022 · 3 comments
Closed

Resolving deprecation of findDOMNode in StrictMode #693

mustaphaboudouch opened this issue Dec 28, 2022 · 3 comments

Comments

@mustaphaboudouch
Copy link

mustaphaboudouch commented Dec 28, 2022

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>
@carlbrn
Copy link

carlbrn commented Apr 9, 2023

#685
It's answered in changelog https://github.com/react-grid-layout/react-draggable/blob/master/CHANGELOG.md#440-may-12-2020

@STRML
Copy link
Collaborator

STRML commented Sep 27, 2023

Closing, see nodeRef in README.

@STRML STRML closed this as completed Sep 27, 2023
@rhyek
Copy link

rhyek commented Apr 27, 2025

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants