通过ceact-react-app搭建的React项目,在使用antd-mobile组件库中的Carousel(跑马灯)组件时,报如下错误:
<font color="red">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:</font > https://fb.me/react-strict-mode-string-ref
<font color="red">
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: </font > https://fb.me/react-strict-mode-string-ref
(该报错并不影响项目的运行以及跑马灯的效果)
根据报错信息的提示:一个字符串ref,“slider”,已经在一个严格的模式树中找到。说明在React中使用的是严格模式了。
然后自然就想到了 React中的严格模式(Strict Mode)。
根据以上信息,找到项目src目录中的index.js,删除掉该 <React.StrictMode>标签即可。如下图所示:
报错即可完美解决。 ؏؏☝ᖗ乛◡乛ᖘ☝؏؏