You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 :
classDraggableCoreextendsReact.Component<DraggableCoreProps,DraggableCoreState>{element: any=typeofthis.props.innerRef==='function'
? {current: null}
: React.createRef<HTMLElement>();// Now w'll use `getElement` instead of `findDOMNode`getElement=()=>(this.props.innerRef&&typeofthis.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;returnReact.cloneElement(React.Children.only(children),{ref: typeofinnerRef==='function'
? (current: HTMLElement)=>{innerRef(current);this.element.current=current;}
: innerRef||this.element,
...props,});}}
And here is a simple usage :
constmyExternalRef=useRef(null)// `innerRef` is optional. If it's not passed, DraggableCore will create an internal ref<DraggableCoreinnerRef={myExternalRef}><span>Test</span></DraggableCore>
The text was updated successfully, but these errors were encountered:
Problem
Getting this warning when using the deprecated function
findDOMNode
inStrictMode
.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 aninnerRef
. 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 :
And here is a simple usage :
The text was updated successfully, but these errors were encountered: