網(wǎng)站首頁 編程語言 正文
一、簡介
Hook是React 16.8.0版本增加的新特性/新語法
可以在函數(shù)組件中使用 state 以及其他的 React 特性
二、使用
1、State Hook
(1)State Hook讓函數(shù)組件也可以有state狀態(tài), 并進(jìn)行狀態(tài)數(shù)據(jù)的讀寫操作
(2)語法: const [xxx, setXxx] = React.useState(initValue)
(3)useState()說明:
參數(shù): 第一次初始化指定的值在內(nèi)部作緩存
返回值: 包含2個(gè)元素的數(shù)組, 第1個(gè)為內(nèi)部當(dāng)前狀態(tài)值, 第2個(gè)為更新狀態(tài)值的函數(shù)
(4)setXxx()2種寫法:
setXxx(newValue): 參數(shù)為非函數(shù)值, 直接指定新的狀態(tài)值, 內(nèi)部用其覆蓋原來的狀態(tài)值
setXxx(value => newValue): 參數(shù)為函數(shù), 接收原本的狀態(tài)值, 返回新的狀態(tài)值, 內(nèi)部用其覆蓋原來的狀態(tài)值
用類式組件如下:
export default class Demo extends Component { state = {count:0} add = () => { this.setState(state => ({count: state.count + 1})) } render() { return ( <div> <h2>當(dāng)前求和為:{this.state.count}</h2> <button onClick={this.add}>點(diǎn)我加1</button> </div> ) } }
用函數(shù)式組件如下:
export default function Demo() { const [count,setCount] = React.useState(0) // 其他狀態(tài)也要用同樣的方法 const [name,setName] = React.useState('Jack') function add() { // 寫法一 // setCount(count + 1) // 寫法二 setCount(count => count + 1) } return ( <div> <h2>當(dāng)前求和為:{this.state.count}</h2> <button onClick={add}>點(diǎn)我加1</button> </div> ) }
Demo
函數(shù)調(diào)用1+n
次,每次調(diào)用const [count,setCount] = React.useState(0)
都會(huì)執(zhí)行,理論上count
的值一直都是0,但實(shí)際上每次點(diǎn)擊按鈕count
的值都會(huì)加1。那是因?yàn)?code>React底層做了處理,第一次調(diào)用的時(shí)候就把count
的值存下來了,后續(xù)再調(diào)用函數(shù)的時(shí)候不會(huì)因?yàn)閭魅氲氖?code>0把原來的值給覆蓋掉。
2、Effect Hook
(1)Effect Hook 可以讓你在函數(shù)組件中執(zhí)行副作用操作(用于模擬類組件中的生命周期鉤子)
(2)React中的副作用操作:
發(fā)ajax請(qǐng)求數(shù)據(jù)獲取
設(shè)置訂閱 / 啟動(dòng)定時(shí)器
手動(dòng)更改真實(shí)DOM
(3)語法和說明:
useEffect(() => { // 在此可以執(zhí)行任何帶副作用操作 return () => { // 在組件卸載前執(zhí)行 // 在此做一些收尾工作, 比如清除定時(shí)器/取消訂閱等 } }, [stateValue]) // 如果指定的是[], 回調(diào)函數(shù)只會(huì)在第一次render()后執(zhí)行
(4)可以把 useEffect Hook 看做如下三個(gè)函數(shù)的組合
componentDidMount()
componentDidUpdate()
componentWillUnmount()?
用類式組件如下:
export default class Demo extends Component { state = {count:0} add = () => { this.setState(state => ({count: state.count + 1})) } unmount = () => { ReactDOM.unmountComponentAtNode(document.getElementById('root')) } componentDidMount() { this.timer = setInterval(() => { this.setState(state => ({count:state.count + 1})) },1000) } componentWillUnmount(){ clearInterval(this.timer) } render() { return ( <div> <h2>當(dāng)前求和為:{this.state.count}</h2> <button onClick={this.add}>點(diǎn)我加1</button> <button onClick={this.unmount}>卸載組件</button> </div> ) } }
用函數(shù)式組件如下:
export default function Demo() { const [count,setCount] = React.useState(0) React.useEffect(() => { let timer = setInterval(() => { setCount(count=> count + 1) },1000) return () => { clearInterval(timer) } },[]) function add() { setCount(count => count + 1) } function unmount() { ReactDOM.unmountComponentAtNode(document.getElementById('root')) } return ( <div> <h2>當(dāng)前求和為:{this.state.count}</h2> <button onClick={add}>點(diǎn)我加1</button> <button onClick={unmount}>卸載組件</button> </div> ) }
(1)React.useEffect(() => {})
,如果第二個(gè)參數(shù)沒有寫,會(huì)監(jiān)測所有的狀態(tài),初始化以及每個(gè)狀態(tài)變化的時(shí)候都會(huì)調(diào)用,相當(dāng)于componentDidMount
和componentDidUpdate
(2)React.useEffect(() => {},[])
,如果第二個(gè)參數(shù)是空數(shù)組,所有的狀態(tài)都不會(huì)監(jiān)測,執(zhí)行1
次(初始化),相當(dāng)于componentDidMount
(3)React.useEffect(() => {},[count])
,監(jiān)測count
值,執(zhí)行1+n
次(初始化以及count值變化的時(shí)候)
(4)React.useEffect(() => {})
中返回一個(gè)函數(shù),相當(dāng)于componentWillUnmount
3、Ref Hook
(1)Ref Hook可以在函數(shù)組件中存儲(chǔ)/查找組件內(nèi)的標(biāo)簽或任意其它數(shù)據(jù)
(2)語法: const refContainer = useRef()
(3)作用:保存標(biāo)簽對(duì)象,功能與React.createRef()一樣
用類式組件如下:
export default class Demo extends Component { myRef = React.createRef() show = ()=>{ alert(this.myRef.current.value) } render() { return ( <div> <input type="text" ref={this.myRef}/> <button onClick={this.show }>點(diǎn)擊提示數(shù)據(jù)</button> </div> ) } }
用函數(shù)式組件如下:
export default class Demo extends Component { const myRef = React.useRef() function show() { alert(myRef.current.value) } render() { return ( <div> <input type="text" ref={myRef}/> <button onClick={show }>點(diǎn)擊提示數(shù)據(jù)</button> </div> ) } }
原文鏈接:https://blog.csdn.net/m0_46613429/article/details/128456573
相關(guān)推薦
- 2022-08-17 create-react-app項(xiàng)目配置全解析_React
- 2022-09-06 Python實(shí)現(xiàn)周日歷與時(shí)間相互轉(zhuǎn)換_python
- 2022-03-27 Linux上搭載Nginx負(fù)載均衡配置使用案例詳解_nginx
- 2022-11-28 Kotlin的Collection與Sequence操作異同點(diǎn)詳解_Android
- 2023-05-21 numpy.unique()使用方法_python
- 2022-04-25 C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)_C#教程
- 2022-05-02 C語言實(shí)現(xiàn)簡單回聲服務(wù)器_C 語言
- 2022-11-17 VMware?vSphere?ESXi系統(tǒng)設(shè)置靜態(tài)IP的方法_VMware
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支