React中常用的Hook有哪些
一、簡(jiǎn)介
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)測(cè)所有的狀態(tài),初始化以及每個(gè)狀態(tài)變化的時(shí)候都會(huì)調(diào)用,相當(dāng)于componentDidMount
和componentDidUpdate
(2)React.useEffect(() => {},[])
,如果第二個(gè)參數(shù)是空數(shù)組,所有的狀態(tài)都不會(huì)監(jiān)測(cè),執(zhí)行1
次(初始化),相當(dāng)于componentDidMount
(3)React.useEffect(() => {},[count])
,監(jiān)測(cè)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> ) } }
到此這篇關(guān)于React中常用的Hook有哪些的文章就介紹到這了,更多相關(guān)React Hook內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Remix 后臺(tái)桌面開發(fā)electron-remix-antd-admin
這篇文章主要為大家介紹了Remix 后臺(tái)桌面開發(fā)electron-remix-antd-admin的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04React中useState原理的代碼簡(jiǎn)單實(shí)現(xiàn)
要實(shí)現(xiàn)useState的背后原理,則需要深入了解狀態(tài)是如何在函數(shù)組件的渲染周期中保持和更新的,本文將通過一段代碼簡(jiǎn)單闡述useState鉤子函數(shù)的實(shí)現(xiàn)思路,希望對(duì)大家有所幫助2023-12-12react.js CMS 刪除功能的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猺eact.js CMS 刪除功能的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04React-hook-form-mui基本使用教程(入門篇)
react-hook-form-mui可以幫助開發(fā)人員更輕松地構(gòu)建表單,它結(jié)合了React?Hook?Form和Material-UI組件庫(kù),使用react-hook-form-mui,開發(fā)人員可以更快速地構(gòu)建表單,并且可以輕松地進(jìn)行表單驗(yàn)證和數(shù)據(jù)處理,本文介紹React-hook-form-mui基本使用,感興趣的朋友一起看看吧2024-02-02axios請(qǐng)求響應(yīng)數(shù)據(jù)加解密封裝實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了axios請(qǐng)求響應(yīng)數(shù)據(jù)加解密封裝實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03