第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:函數(shù)組件不能有字符串引用。我們建議改用 useRef()

錯誤:函數(shù)組件不能有字符串引用。我們建議改用 useRef()

慕田峪4524236 2022-12-22 14:44:54
我在我的 React 應(yīng)用程序中使用 ace Editor 并收到上述錯誤。我想將我在我的 React 應(yīng)用程序中使用的 Ace Editor IDE 的內(nèi)容放在通過提交按鈕觸發(fā)的函數(shù)調(diào)用中。<AceEditorref='aceEditor'height='100%'width='100%'mode={ideLanguage}theme={ideTheme}fontSize={ideFontSize}showGutter={true}showPrintMargin={false}/>這是按鈕<Buttontype='button'buttonStyle='btn--primary--normal'buttonSize='btn--medium'onClick={SendCode}>SUBMIT</Button>這是功能const SendCode = () => {  console.log(this.refs.aceEditor.editor.getValue());};我究竟做錯了什么??
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

字符串引用是設(shè)置 DOM 引用的傳統(tǒng)方式。

在最新的 React 版本中,建議對功能組件和類組件使用React.useRef()鉤子。React.createRef()

您可以閱讀更多詳細信息 - https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs

我猜,你可能已經(jīng)打開了<React.StrictMode>高階組件的嚴格模式。這就是為什么會拋出錯誤/警告的原因。

你應(yīng)該做什么 -

  1. 聲明一個 ref 變量。

    const aceEditorRef = useRef();

  2. 之后,替換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());

};


查看完整回答
反對 回復(fù) 2022-12-22
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號