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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 React 中使用 useRef

在 React 中使用 useRef

嗶嗶one 2023-07-14 10:26:46
const App = () => {    function hideCharacter(){        const randomChar = useRef(null);        randomChar.classList.add('hide');    }    return (        <>             <RandomChar ref={randomChar} /> // error: not defined            <button class="btn btn-primary" onClick={hideCharacter()}>Load another one</button>        </>    );};為什么我的引用沒有定義?我需要將代碼重構(gòu)為類嗎?
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

你違反了 React Hook 規(guī)則。鉤子應(yīng)該在頂部的功能組件中聲明。


在您的情況下,您在本地函數(shù)內(nèi)聲明它,并且在該函數(shù)之外不可用。


它必須是這樣的:


const App = () => {

    const randomChar = useRef(null); // <---declare it here

    function hideCharacter(){        

        randomChar.classList.add('hide');

    }

    return (

        <> 

            <RandomChar ref={randomChar} /> // error: not defined

            <button class="btn btn-primary" onClick={hideCharacter()}>Load another one</button>

        </>

    );

};


查看完整回答
反對(duì) 回復(fù) 2023-07-14
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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