我在我的应用程序中使用ace编辑器,并收到上述错误。我想将我的Ace Editor IDE的内容放在通过提交按钮触发的函数调用中的我的react应用程序中。
<AceEditor
ref='aceEditor'
height='100%'
width='100%'
mode={ideLanguage}
theme={ideTheme}
fontSize={ideFontSize}
showGutter={true}
showPrintMargin={false}/>
这是按钮
<Button
type='button'
buttonStyle='btn--primary--normal'
buttonSize='btn--medium'
onClick={SendCode}
>SUBMIT</Button>
这是函数
const SendCode = () => {
console.log(this.refs.aceEditor.editor.getValue());
};
我在做什么错??
答案 0 :(得分:0)
字符串引用是设置DOM引用的传统方式。
在最新的React版本中,建议对功能组件使用React.useRef()
挂钩,对类组件使用React.createRef()
。
您可以在以下位置阅读更多详细信息: https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs
我可以猜测,您可能已经使用<React.StrictMode>
个高阶组件打开了严格模式。这就是为什么会引发错误/警告的原因。
您应该做什么-
声明一个引用变量。
const aceEditorRef = useRef();
然后,将ref='aceEditor'
替换为ref={aceEditorRef}
。
<AceEditor
ref={aceEditorRef}
height='100%'
width='100%'
mode={ideLanguage}
theme={ideTheme}
fontSize={ideFontSize}
showGutter={true}
showPrintMargin={false}/>
使用aceEditorRef.current获取DOM引用
const SendCode = () => {
console.log(this.aceEditorRef.current.editor.getValue());
};