解決React?hook?'useState'?cannot?be?called?in?a?class?component報(bào)錯
總覽
當(dāng)我們嘗試在類組件中使用useState 鉤子時(shí),會產(chǎn)生"React hook 'useState' cannot be called in a class component"錯誤。為了解決該錯誤,請將類組件轉(zhuǎn)換為函數(shù)組件。因?yàn)殂^子不能在類組件中使用。

這里有個(gè)例子用來展示錯誤是如何發(fā)生的。
// App.js
import {useState, useEffect} from 'react';
class Example {
render() {
// ?? React Hook "useState" cannot be called in a class component.
// React Hooks must be called in a React function component or a custom React Hook function.
const [count, setCount] = useState(0);
// ?? React Hook "useEffect" cannot be called in a class component.
// React Hooks must be called in a React function component or a custom React Hook function.
useEffect(() => {
console.log('hello world');
}, []);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
}
導(dǎo)致這個(gè)錯誤的原因是,鉤子只能在函數(shù)組件或自定義鉤子中使用,而我們正試圖在一個(gè)類中使用鉤子。
函數(shù)組件
解決該錯誤的一種方法是,將類組件轉(zhuǎn)換為函數(shù)組件。
// App.js
import {useState, useEffect} from 'react';
export default function App() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('hello world');
}, []);
return (
<div>
<h2>Count {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
就像文檔中所說的那樣:
- 只從React函數(shù)組件或自定義鉤子中調(diào)用Hook
- 只在最頂層使用 Hook
- 不要在循環(huán),條件或嵌套函數(shù)中調(diào)用 Hook
- 確??偸窃谀愕?React 函數(shù)的最頂層以及任何 return 之前使用 Hook
類組件中使用setState()
另外,我們可以使用一個(gè)類組件,用setState()方法更新狀態(tài)。
// App.js
import React from 'react';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
render() {
return (
<div>
<h2>Count: {this.state.count}</h2>
<button onClick={() => this.setState({count: this.state.count + 1})}>
Increment
</button>
</div>
);
}
}
請注意,在較新的代碼庫中,函數(shù)組件比類更常被使用。
它們也更方便,因?yàn)槲覀儾槐乜紤]this關(guān)鍵字,并使我們能夠使用內(nèi)置和自定義鉤子。
翻譯原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React hook 'useState' cannot be called in a class component報(bào)錯的詳細(xì)內(nèi)容,更多關(guān)于React報(bào)錯useState component的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用React實(shí)現(xiàn)一個(gè)簡單的待辦任務(wù)列表
這篇文章主要給大家介紹了使用React和Ant Design庫構(gòu)建的待辦任務(wù)列表應(yīng)用,它包含了可編輯的表格,用戶可以添加、編輯和完成任務(wù),以及保存任務(wù)列表數(shù)據(jù)到本地存儲,文中有相關(guān)的代碼示例,需要的朋友可以參考下2023-08-08
React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼
這篇文章主要介紹了React實(shí)現(xiàn)類似淘寶tab居中切換效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
react中如何使用定義數(shù)據(jù)并監(jiān)聽其值
這篇文章主要介紹了react中如何使用定義數(shù)據(jù)并監(jiān)聽其值問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
react-three/postprocessing庫的參數(shù)中文含義使用解析
這篇文章主要介紹了react-three/postprocessing庫的參數(shù)中文含義使用總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
react18中react-redux狀態(tài)管理的實(shí)現(xiàn)
本文主要介紹了react18中react-redux狀態(tài)管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
React Native react-navigation 導(dǎo)航使用詳解
本篇文章主要介紹了React Native react-navigation 導(dǎo)航使用詳解,詳解的介紹了react-navigation導(dǎo)航的使用,具有一定的參考價(jià)值,有興趣的可以了解一下2017-12-12

