Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of DraggableCore which is inside StrictMode

Draggable package is causing an error in strict mode:

Warning: 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: https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage

Apparently they have never fixed it https://github.com/STRML/react-draggable/issues/440, do you have any nice/elegant solution?

like image 665
Francesco Orsi Avatar asked Aug 26 '20 18:08

Francesco Orsi


People also ask

What is findDOMNode?

findDOMNode is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. It has been deprecated in StrictMode .

What is react StrictMode?

StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants. Note: Strict mode checks are run in development mode only; they do not impact the production build.


1 Answers

According with official git repository on https://github.com/STRML/react-draggable/blob/v4.4.2/lib/DraggableCore.js#L159-L171

/* If running in React Strict mode, ReactDOM.findDOMNode() is deprecated.
* Unfortunately, in order for <Draggable> to work properly, we need raw access
* to the underlying DOM node. If you want to avoid the warning, pass a `nodeRef`
* as in this example:
*/

function MyComponent() {
    const nodeRef = React.useRef(null);
    return (
        <Draggable nodeRef={nodeRef}>
            <div ref={nodeRef}>Example Target</div>
        </Draggable>
    );
}

/*
* This can be used for arbitrarily nested components, so long as the ref ends up
* pointing to the actual child DOM node and not a custom component.
*/

it works!

like image 199
Francesco Orsi Avatar answered Sep 28 '22 00:09

Francesco Orsi