gpt4 book ai didi

javascript - findDOMNode 在 StrictMode 中已弃用。 findDOMNode 被传递了一个 DraggableCore 的实例,该实例位于 StrictMode 内

转载 作者:行者123 更新时间:2023-12-05 00:24:43 26 4
gpt4 key购买 nike

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

Warning: findDOMNode is deprecated in StrictMode. findDOMNode waspassed 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


显然他们从未修复它 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.
*/
有用!

关于javascript - findDOMNode 在 StrictMode 中已弃用。 findDOMNode 被传递了一个 DraggableCore 的实例,该实例位于 StrictMode 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63603902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com