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

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

使用 useeffect 和 useref 時(shí)出錯(cuò) TypeError:無法讀取 null 的屬性

使用 useeffect 和 useref 時(shí)出錯(cuò) TypeError:無法讀取 null 的屬性

您好,我遇到了 userref 問題,我的應(yīng)用程序不斷從我不再訪問的頁面讀取代碼const LandingPage = () => {    useEffect(() => {        document.addEventListener("scroll", () => {            if (window.scrollY < (window.pageYOffset + divRef1.current.getBoundingClientRect().bottom)) {                onHeaderColorSwitch('#c8e9e6')                console.log('green')            } else if (window.scrollY >= (window.pageYOffset + divRef2.current.getBoundingClientRect().top) && window.scrollY < (window.pageYOffset + divRef2.current.getBoundingClientRect().bottom)) {                onHeaderColorSwitch('#ffae5a')            } else if (window.scrollY >= (window.pageYOffset + divRef3.current.getBoundingClientRect().top) && window.scrollY < (window.pageYOffset + divRef3.current.getBoundingClientRect().bottom)) {            }        })    }, [])}我有這個(gè)代碼,但是當(dāng)我使用這個(gè)更改到聯(lián)系頁面時(shí)function App() {    let routes =<Switch>   <Route path="/" exact component={landingPage}/>   <Route path="/contact" exact component={contactPage}/>    </Switch>然后當(dāng)我嘗試滾動(dòng)新頁面時(shí),我收到此錯(cuò)誤代碼TypeError: Cannot read property 'getBoundingClientRect' of nullHTMLDocument.<anonymous>my-app/src/screens/landingPage.js:22  19 |   20 | useEffect(() => {  21 |     document.addEventListener("scroll", () => {> 22 |         if (window.scrollY < (window.pageYOffset + divRef1.current.getBoundingClientRect().bottom)) {     | ^  23 |             onHeaderColorSwitch('#c8e9e6')  24 |   25 |         } else if (window.scrollY >= (window.pageYOffset + divRef2.current.getBoundingClientRect().top) &&  window.scrollY < (window.pageYOffset + divRef2.current.getBoundingClientRect().bottom)) {即使我現(xiàn)在位于新頁面,事件偵聽器仍在偵聽。一旦刷新頁面,該錯(cuò)誤就不會(huì)影響我,現(xiàn)在和將來如何防止這種情況發(fā)生?
查看完整描述

1 回答

?
慕姐4208626

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

您需要在 useEffect 回調(diào)函數(shù)中刪除監(jiān)聽器:


useEffect(() => {

? const listener = () => {

? ? ?if (window.scrollY < (window.pageYOffset + divRef1.current.getBoundingClientRect().bottom)) {

? ? ? ? ?onHeaderColorSwitch('#c8e9e6')

? ? ? ? ?console.log('green')

? ? ?} else if (window.scrollY >= (window.pageYOffset + divRef2.current.getBoundingClientRect().top) &&? window.scrollY < (window.pageYOffset + divRef2.current.getBoundingClientRect().bottom)) {

? ? ? ? ?onHeaderColorSwitch('#ffae5a')

? ? ?} else if (window.scrollY >= (window.pageYOffset + divRef3.current.getBoundingClientRect().top) &&? window.scrollY < (window.pageYOffset + divRef3.current.getBoundingClientRect().bottom)) {

? ? ?}

? }

? document.addEventListener("scroll", listener);

? return () => {

? ? // Clean up the subscription

? ? document.removeEventListener(listener);

? };

}, []);

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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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