這篇文章主要為大家展示了“React優(yōu)化子組件render怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“React優(yōu)化子組件render怎么用”這篇文章吧。
創(chuàng)新互聯(lián)公司長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為泗陽企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,泗陽網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。在react中,父組件的重新render會(huì)引發(fā)子組件的重新render,但是一些情況下我們會(huì)覺得這樣做有些多余,比如:
父組件并未傳遞props給子組件
新傳遞的props渲染結(jié)果不變
class A extends React.Component { render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { render() { return ( <div> // 點(diǎn)擊button會(huì)讓A不斷調(diào)用render <button onClick={() => this.setState({ a: 1 })}>Main</button> <A /> </div> ) } }
為了解決這個(gè)問題,需要分為ES6類組件和函數(shù)式組件兩種:
類組件
使用shouldComponentUpdate
來對(duì)props和state進(jìn)行判斷以此決定是否進(jìn)行render
class A extends React.Component { shouldComponentUpdate(nextProps, nextState) { //兩次props對(duì)比 return nextProps.a === this.props.a ? false : true } render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { // ... render() { return ( <div> <button onClick={() => this.setState({ a: 1 })}>Main</button> <A a={this.state.a} /> </div> ) } }
通過返回false來跳過這次更新
使用React.PureComponent
,它與React.Component
區(qū)別在于它已經(jīng)內(nèi)置了shouldComponentUpdate
來對(duì)props和state進(jìn)行淺對(duì)比,并跳過更新
//PureComponent class A extends React.PureComponent { render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { state = { a: 1 } render() { return ( <div> <button onClick={() => this.setState({ a: 1 })}>Main</button> <A a={this.state.a} /> </div> ) } }
函數(shù)組件
使用高階組件React.memo
來包裹函數(shù)式組件,它和類組件的PureComponent類似,也是對(duì)對(duì)props進(jìn)行淺比較決定是否更新
const A = props => { console.log('render A') return <div>這是A組件</div> } // React.memo包裹A const B = React.memo(A) const Main = props => { const [a, setA] = useState(1) console.log('render Main') return ( <div> // 通過setA(a + 1)讓父組件重新render <button onClick={() => setA(a + 1)}>Main</button> // 一直傳入相同的props不會(huì)讓子組件重新render <B a={1} /> </div> ) }
它的第二個(gè)參數(shù)接受一個(gè)兩次props作為參數(shù)的函數(shù),返回true則禁止子組件更新
其他
上面提到的淺比較就是根據(jù)內(nèi)存地址判斷是否相同:
// extends React.Component class A extends React.Component { render() { console.log('render A') console.log(this.props) return <div>這是組件A</div> } } class Main extends React.Component { test = [1, 2, 3] render() { console.log('render Main') return ( <div> <button onClick={() => { // 父組件render this.setState({}) this.test.push(4) }} > Main </button> <A test={this.test} /> </div> ) } }
結(jié)果是:
使用React.component:
使用React.PureComponent:
使用React.component,點(diǎn)擊之后子組件重新render。改為React.PureComponent之后,點(diǎn)擊button子組件并不會(huì)render。也因此,PureComponent根據(jù)前后內(nèi)存地址判斷是否相等,所以向子組件傳遞函數(shù)作為props時(shí),使用內(nèi)聯(lián)箭頭函數(shù)的形式將會(huì)導(dǎo)致子組件的重新render;所以可以用箭頭函數(shù)作為成員變量的形式再將函數(shù)引用作為props傳遞。
以上是“React優(yōu)化子組件render怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:React優(yōu)化子組件render怎么用-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://m.rwnh.cn/article46/cedgeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App開發(fā)、網(wǎng)頁設(shè)計(jì)公司、虛擬主機(jī)、面包屑導(dǎo)航、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容