網(wǎng)站首頁 編程語言 正文
解決React?hook?'useState'?cannot?be?called?in?a?class?component報錯_React
作者:Borislav?Hadzhiev ? 更新時間: 2022-12-29 編程語言總覽
當我們嘗試在類組件中使用useState
鉤子時,會產(chǎn)生"React hook 'useState' cannot be called in a class component"錯誤。為了解決該錯誤,請將類組件轉(zhuǎn)換為函數(shù)組件。因為鉤子不能在類組件中使用。
這里有個例子用來展示錯誤是如何發(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)致這個錯誤的原因是,鉤子只能在函數(shù)組件或自定義鉤子中使用,而我們正試圖在一個類中使用鉤子。
函數(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()
另外,我們可以使用一個類組件,用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ù)組件比類更常被使用。
它們也更方便,因為我們不必考慮this
關(guān)鍵字,并使我們能夠使用內(nèi)置和自定義鉤子。
翻譯原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7135077641073197093
相關(guān)推薦
- 2022-07-12 CSS樣式:行內(nèi)元素 盒子模型
- 2022-11-30 Python實現(xiàn)解析命令行參數(shù)的常見方法總結(jié)_python
- 2023-01-26 Python上下文管理器深入講解_python
- 2022-03-29 python教程之生成器和匿名函數(shù)_python
- 2024-03-05 layui彈出層的表單驗證(form表單自帶的驗證不執(zhí)行)
- 2022-07-24 C#導(dǎo)入和導(dǎo)出CSV文件_C#教程
- 2022-04-15 python3?cmp實現(xiàn)方式_python
- 2022-04-03 Pytorch寫數(shù)字識別LeNet模型_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支