這篇文章主要講解了“react性能優(yōu)化的周期函數(shù)是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“react性能優(yōu)化的周期函數(shù)是什么”吧!
創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),先為龍馬潭等服務(wù)建站,龍馬潭等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為龍馬潭企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
react性能優(yōu)化是shouldComponentUpdate周期函數(shù);該函數(shù)可判斷是否需要調(diào)用render方法重新描繪dom,能夠優(yōu)化dom diff算法,語(yǔ)法為“shouldComponentUpdate(Props,state)”。
本教程操作環(huán)境:Windows10系統(tǒng)、react16.4.0版、Dell G3電腦。
shouldComponentUpdate 這個(gè)方法用來(lái)判斷是否需要調(diào)用render方法重新描繪dom。因?yàn)閐om的描繪非常消耗性能,如果我們能在shouldComponentUpdate方法中能夠?qū)懗龈鼉?yōu)化的dom diff算法,可以極大的提高性能
shouldComponentUpdate() 方法格式如下:
shouldComponentUpdate(nextProps, nextState)
shouldComponentUpdate() 方法會(huì)返回一個(gè)布爾值,指定 React 是否應(yīng)該繼續(xù)渲染,默認(rèn)值是 true, 即 state 每次發(fā)生變化組件都會(huì)重新渲染。
shouldComponentUpdate() 的返回值用于判斷 React 組件的輸出是否受當(dāng)前 state 或 props 更改的影響,當(dāng) props 或 state 發(fā)生變化時(shí),shouldComponentUpdate() 會(huì)在渲染執(zhí)行之前被調(diào)用。
以下實(shí)例演示了 shouldComponentUpdate() 方法返回 false 時(shí)執(zhí)行的操作(點(diǎn)擊按鈕無(wú)法修改):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React 實(shí)例</title> <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="root"></div> <script type="text/babel"> class Header extends React.Component { constructor(props) { super(props); this.state = {favoritesite: "runoob"}; } shouldComponentUpdate() { return false; } changeSite = () => { this.setState({favoritesite: "google"}); } render() { return ( <div> <h2>我喜歡的網(wǎng)站是 {this.state.favoritesite}</h2> <button type="button" onClick={this.changeSite}>修改</button> </div> ); } } ReactDOM.render(<Header />, document.getElementById('root')); </script> </body> </html>
輸出結(jié)果:
以下實(shí)例演示了 shouldComponentUpdate() 方法返回 true 時(shí)執(zhí)行的操作(點(diǎn)擊按鈕可以修改):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React 實(shí)例</title> <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="root"></div> <script type="text/babel"> class Header extends React.Component { constructor(props) { super(props); this.state = {favoritesite: "runoob"}; } shouldComponentUpdate() { return true; } changeSite = () => { this.setState({favoritesite: "google"}); } render() { return ( <div> <h2>我喜歡的網(wǎng)站是 {this.state.favoritesite}</h2> <button type="button" onClick={this.changeSite}>修改</button> </div> ); } } ReactDOM.render(<Header />, document.getElementById('root')); </script> </body> </html>
輸出結(jié)果:
點(diǎn)擊按鈕后:
感謝各位的閱讀,以上就是“react性能優(yōu)化的周期函數(shù)是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)react性能優(yōu)化的周期函數(shù)是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
文章名稱:react性能優(yōu)化的周期函數(shù)是什么
分享鏈接:http://www.rwnh.cn/article38/jiscpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、Google、網(wǎng)站營(yíng)銷、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)