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

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

在反應(yīng)應(yīng)用程序中修復(fù)此“無(wú)效掛鉤呼叫”錯(cuò)誤的正確方法是什么?

在反應(yīng)應(yīng)用程序中修復(fù)此“無(wú)效掛鉤呼叫”錯(cuò)誤的正確方法是什么?

桃花長(zhǎng)相依 2022-05-26 14:36:03
好吧,我有這個(gè)錯(cuò)誤Error: Invalid hook call. Hooks can only be called inside of the body of a function component. 我嘗試了很多不同的選項(xiàng)來(lái)解決這個(gè)問(wèn)題,但我失敗了。這是我的代碼export const DataInput = () => {    const Post = (testTitle, testText) => {            useFirestore().collection('schedule-data').doc('test').set({                testTitle: testTitle,                testText: testText            })         }    return(        <Button            variant="primary"           onClick={()=> Post(testTitle, testText)}>           POST data        </Button>刪除了一些無(wú)關(guān)緊要的代碼
查看完整描述

3 回答

?
冉冉說(shuō)

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

Hooks 只能在渲染組件時(shí)調(diào)用,因此它們需要位于組件的主體中。


export const DataInput = () => {

  const firestore = useFirestore();

  const Post = (testTitle, testText) => {

    firestore.collection('schedule-data').doc('test').set({

      testTitle: testTitle,

      testText: testText

    })

  }

  // etc

}


查看完整回答
反對(duì) 回復(fù) 2022-05-26
?
函數(shù)式編程

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

不要在循環(huán)、條件或嵌套函數(shù)中調(diào)用 Hook。相反,請(qǐng)始終在 React 函數(shù)的頂層使用 Hooks。通過(guò)遵循此規(guī)則,您可以確保每次渲染組件時(shí)都以相同的順序調(diào)用 Hook。這就是允許 React 在多個(gè) useState 和 useEffect 調(diào)用之間正確保留 Hooks 狀態(tài)的原因。(如果你好奇,可以在這里找到解釋)



查看完整回答
反對(duì) 回復(fù) 2022-05-26
?
素胚勾勒不出你

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

根據(jù)您的代碼示例,我可能會(huì)建議testTitle, testText以DataInput某種方式提供,因此您可以使用useCallback. React 將創(chuàng)建回調(diào)以用作處理程序,并且僅在testTitle, testText更改時(shí)重新創(chuàng)建。


import {useCallback} from 'react';


export const DataInput = () => {

    const makePost = useCallback(() => {

      useFirestore().collection('schedule-data').doc('test').set({

        testTitle: testTitle,

        testText: testText

      })

    }, [testTitle, testText]);


    return (

    <Button

      variant="primary"

      onClick={makePost}

      {/* Avoid inline callback  declaration */}

    >

      POST data

    </Button>

    )

}


查看完整回答
反對(duì) 回復(fù) 2022-05-26
  • 3 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(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)