本文介绍了在StrictMode中,findDOMNode已弃用。向findDOMNode传递了StrictMode内部的DraggableCore的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可拖动包在严格模式下导致错误:
显然他们从来没有修复过https://github.com/STRML/react-draggable/issues/440,你们有什么好的/优雅的解决方案吗?
推荐答案
根据https://github.com/STRML/react-draggable/blob/v4.4.2/lib/DraggableCore.js#L159-L171上的官方Git存储库
/* 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.
*/
起作用了!
这篇关于在StrictMode中,findDOMNode已弃用。向findDOMNode传递了StrictMode内部的DraggableCore的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!