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è)字母大寫,或者使用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>
);
}
上述代碼片段的問題在于,我們?cè)谝粋€(gè)函數(shù)中使用了useEffect鉤子,而這個(gè)函數(shù)不是一個(gè)組件,因?yàn)樗孕懽帜搁_頭,也不是一個(gè)自定義鉤子,因?yàn)樗拿植皇且?code>use開頭。
聲明組件
如果你想聲明一個(gè)組件,請(qǐng)將你的函數(shù)的第一個(gè)字母大寫。
// 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ù)名必須以大寫字母開頭,因?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è)自定義鉤子,自定義鉤子的名稱必須以use開頭,比如說(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開頭,這樣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)文章!
相關(guān)文章
React Native開發(fā)封裝Toast與加載Loading組件示例
這篇文章主要介紹了React Native開發(fā)封裝Toast與加載Loading組件,小編覺得挺不錯(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-12
React中hook函數(shù)與useState及useEffect的使用
這篇文章主要介紹了React中hook函數(shù)與useState及useEffect的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
react-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-09
React學(xué)習(xí)筆記之高階組件應(yīng)用
這篇文章主要介紹了React 高階組件應(yīng)用,詳細(xì)的介紹了什么是React高階組件和具體使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

