通过ceact-react-app搭建的React项目,在使用antd-mobile组件库中的Carousel(跑马灯)组件时,报如下错误:

Warning: A string ref, “slider”, has been found within a strict mode tree. String refs are a source of potential bugs and should be avoided. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://fb.me/react-strict-mode-string-ref
Warning: A string ref, “frame”, has been found within a strict mode tree. String refs are a source of potential bugs and should be avoided. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://fb.me/react-strict-mode-string-ref
(该报错并不影响项目的运行以及跑马灯的效果)
根据报错信息的提示:一个字符串ref,“slider”,已经在一个严格的模式树中找到。说明在React中使用的是严格模式了。
然后自然就想到了 React中的严格模式(Strict Mode)。

根据以上信息,找到项目src目录中的index.js,删除掉该 <React.StrictMode>标签即可。如下图所示:

报错即可完美解决。 ؏؏☝ᖗ乛◡乛ᖘ☝؏؏
本文解决在使用Create-React-App搭建的React项目中,Antd Mobile的Carousel组件出现的StrictMode警告问题。通过移除React.StrictMode标签,可以消除关于字符串ref的警告,不影响项目运行及组件效果。

2596

被折叠的 条评论
为什么被折叠?



