React Hook 'useEffect' is called in function報(bào)錯(cuò)解決
總覽
為了解決錯(cuò)誤"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function",可以將函數(shù)名的第一個(gè)字母大寫(xiě),或者使用use
作為函數(shù)名的前綴。比如說(shuō),useCounter
使其成為一個(gè)組件或一個(gè)自定義鉤子。
這里有個(gè)示例用來(lái)展示錯(cuò)誤是如何發(fā)生的。
// App.js import React, {useEffect, useState} from 'react'; // ??? Not a component (lowercase first letter) // not a custom hook (doesn't start with use) function counter() { const [count, setCount] = useState(0); // ?? React Hook "useEffect" is called in function "counter" that // is neither a React function component nor a custom React Hook function. // React component names must start with an uppercase letter. // React Hook names must start with the word "use". useEffect(() => { console.log(count); }, [count]); return ( <div> <h2>Count: {count}</h2> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }
上述代碼片段的問(wèn)題在于,我們?cè)谝粋€(gè)函數(shù)中使用了useEffect
鉤子,而這個(gè)函數(shù)不是一個(gè)組件,因?yàn)樗孕?xiě)字母開(kāi)頭,也不是一個(gè)自定義鉤子,因?yàn)樗拿植皇且?code>use開(kāi)頭。
聲明組件
如果你想聲明一個(gè)組件,請(qǐng)將你的函數(shù)的第一個(gè)字母大寫(xiě)。
// App.js import React, {useEffect, useState} from 'react'; // ??? is now a component (can use hooks) function Counter() { const [count, setCount] = useState(0); useEffect(() => { console.log(count); }, [count]); return ( <div> <h2>Count: {count}</h2> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); } export default function App() { return ( <div> <Counter /> </div> ); }
函數(shù)名必須以大寫(xiě)字母開(kāi)頭,因?yàn)檫@有助于React區(qū)分諸如p
、div
、span
等內(nèi)置元素和我們定義的組件。
就像文檔中所說(shuō)的:
- 只從React函數(shù)組件或自定義鉤子中調(diào)用Hook
- 只在最頂層使用 Hook
- 不要在循環(huán),條件或嵌套函數(shù)中調(diào)用 Hook
- 確??偸窃谀愕?React 函數(shù)的最頂層以及任何 return 之前使用 Hook
聲明自定義鉤子
如果你想聲明一個(gè)自定義鉤子,自定義鉤子的名稱(chēng)必須以use
開(kāi)頭,比如說(shuō)useCounter
。
import React, {useEffect, useState} from 'react'; // ??? is a custom hook (name starts with use) function useCounter() { const [count, setCount] = useState(0); useEffect(() => { console.log(count); }, [count]); return [count, setCount]; } export default function App() { const [count, setCount] = useCounter(); return ( <div> <h2>Count: {count}</h2> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }
自定義鉤子的名字必須以use
開(kāi)頭,這樣React才能識(shí)別你的函數(shù)是一個(gè)自定義鉤子。
總結(jié)
為了解決"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function"的錯(cuò)誤,確保只從React函數(shù)組件或自定義鉤子中調(diào)用鉤子。
翻譯原文鏈接:bobbyhadz.com/blog/react-…
以上就是React Hook 'useEffect' is called in function報(bào)錯(cuò)解決的詳細(xì)內(nèi)容,更多關(guān)于React報(bào)錯(cuò)Hook useEffect function的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決React報(bào)錯(cuò)React?Hook?useEffect?has?a?missing?dependency
- React-Hook中使用useEffect清除定時(shí)器的實(shí)現(xiàn)方法
- React函數(shù)式組件Hook中的useEffect函數(shù)的詳細(xì)解析
- React中hook函數(shù)與useState及useEffect的使用
- React?Hooks--useEffect代替常用生命周期函數(shù)方式
- React-hooks中的useEffect使用步驟
- React中useEffect Hook常見(jiàn)問(wèn)題及解決
相關(guān)文章
React.js?Gird?布局編寫(xiě)鍵盤(pán)組件
這篇文章主要介紹了React.js?Gird?布局編寫(xiě)鍵盤(pán)組件,Grid?布局則是將容器劃分成"行"和"列",產(chǎn)生單元格,然后指定"項(xiàng)目所在"的單元格,可以看作是二維布局2022-09-09React Native開(kāi)發(fā)封裝Toast與加載Loading組件示例
這篇文章主要介紹了React Native開(kāi)發(fā)封裝Toast與加載Loading組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09解決React報(bào)錯(cuò)Property value does not exist&n
這篇文章主要為大家介紹了React報(bào)錯(cuò)Property value does not exist on type HTMLElement解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12React中hook函數(shù)與useState及useEffect的使用
這篇文章主要介紹了React中hook函數(shù)與useState及useEffect的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10react-native組件中NavigatorIOS和ListView結(jié)合使用的方法
這篇文章主要給大家介紹了關(guān)于react-native組件中NavigatorIOS和ListView結(jié)合使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09React學(xué)習(xí)筆記之高階組件應(yīng)用
這篇文章主要介紹了React 高階組件應(yīng)用,詳細(xì)的介紹了什么是React高階組件和具體使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06