繁体   English   中英

findDOMNode 在 StrictMode 中已弃用。 findDOMNode 传递了一个 DraggableCore 实例,它在 StrictMode 中

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

Draggable 包在严格模式下导致错误:

警告:在 StrictMode 中不推荐使用findDOMNode findDOMNode传递了一个位于 StrictMode 内的 DraggableCore 实例。 相反,直接向要引用的元素添加 ref。 在此处了解有关安全使用 refs 的更多信息: https : //reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage

显然他们从未修复过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.
*/

有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM