首页 > 解决方案 > 警告:在严格模式树中发现了一个字符串 ref,'code'。字符串引用是潜在错误的来源,应该避免

问题描述

我正在尝试使用 将一些代码片段放在页面中react-highlight,按照其文档,我编写了以下代码:

<Highlight className='javascript'>
{
    <div children={`import React from 'react';
import './Hello.scss';
const Hello = () => {
return (
<>
<div>
Hello world!
</div>
</>
)
}
export default Hello;`}/>

}
</Highlight>

它运行良好并提供语法突出显示。但是,我收到以下控制台警告:

Warning: A string ref, "code", 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. 
    in pre (created by Highlight)
    in Highlight (created by Learning)
    in details (created by Learning)
    in div (created by Learning)
    in div (created by Learning)
    in Learning (created by Layout)
    in Route (created by Layout)
    in Switch (created by Layout)
    in Router (created by HashRouter)
    in HashRouter (created by Layout)
    in Layout
    in StrictMode

我的理解是这是一个由 引起的警告react-highlight,我能做些什么来删除它?

标签: javascriptreactjs

解决方案


推荐阅读