findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of DraggableCore which is inside StrictMode(在StrictMode中,findDOMNode已弃用。向findDOMNode传递了StrictMode内部的DraggableCore的实例)
问题描述
可拖动包在严格模式下导致错误:
警告:findDOMNode在StrictMode中已弃用。findDOMNode是 传递了StrictMode内部的DraggableCore实例。 相反,可以将引用直接添加到要引用的元素。 在此了解有关安全使用裁判的更多信息: 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.
*/
起作用了!
这篇关于在StrictMode中,findDOMNode已弃用。向findDOMNode传递了StrictMode内部的DraggableCore的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在StrictMode中,findDOMNode已弃用。向findDOMNode传递了StrictMode内部的DraggableCore的实例
基础教程推荐
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
