成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

react的生命周期函數(shù)怎么使用

這篇文章主要介紹“react的生命周期函數(shù)怎么使用”,在日常操作中,相信很多人在react的生命周期函數(shù)怎么使用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”react的生命周期函數(shù)怎么使用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到常熟網(wǎng)站設(shè)計(jì)與常熟網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋常熟地區(qū)。

react的生命周期函數(shù)有:1、componentWillMount函數(shù);2、componentDidMount函數(shù);3、shouldComponentUpdate函數(shù);4、componentWillUpdate函數(shù);5、componentDidUpdate函數(shù);6、componentWillUnmount函數(shù);7、componentWillReceiveProps函數(shù)。

先來(lái)了解一下react的生命周期函數(shù)有哪些:

  • 組件將要掛載時(shí)觸發(fā)的函數(shù):componentWillMount

  • 組件掛載完成時(shí)觸發(fā)的函數(shù):componentDidMount

  • 是否要更新數(shù)據(jù)時(shí)觸發(fā)的函數(shù):shouldComponentUpdate

  • 將要更新數(shù)據(jù)時(shí)觸發(fā)的函數(shù):componentWillUpdate

  • 數(shù)據(jù)更新完成時(shí)觸發(fā)的函數(shù):componentDidUpdate

  • 組件將要銷毀時(shí)觸發(fā)的函數(shù):componentWillUnmount

  • 父組件中改變了props傳值時(shí)觸發(fā)的函數(shù):componentWillReceiveProps

下面來(lái)上代碼詳細(xì)說(shuō)明一下

一.掛載部分
根據(jù)官方生命周期圖我們可以看到,一個(gè)組件的加載渲染,首先是defaultProps和propsTypes然后就是constructor及this.state里的初始數(shù)據(jù),所以到這里是第一步。接著就是componentWillMount 組件將要開(kāi)始掛載了,這是第二步。然后組件掛載,render解析渲染,所以第三步呼之欲出,就是render數(shù)據(jù)都渲染完成,最后componentDidMount
組件掛載完成。

子組件代碼,父組件內(nèi)引入渲染即可(這里先不上代碼)

import React ,{Component} from 'react'

class Smzq extends Component{
constructor(props){
console.log('01構(gòu)造函數(shù)')
super(props)
this.state={

}
}
//組件將要掛載時(shí)候觸發(fā)的生命周期函數(shù)
componentWillMount(){
console.log('02組件將要掛載')
}
//組件掛載完成時(shí)候觸發(fā)的生命周期函數(shù)
componentDidMount(){
console.log('04組件將要掛載')
}
render(){
console.log('03數(shù)據(jù)渲染render')
return(
<div>
生命周期函數(shù)演示
</div>
)
}
}
export default Smzq

打開(kāi)控制臺(tái)查看
react的生命周期函數(shù)怎么使用

二.數(shù)據(jù)更新部分
數(shù)據(jù)更新的話第一步是shouldComponentUpdate確認(rèn)是否要更新數(shù)據(jù),當(dāng)這個(gè)函數(shù)返回的是true的時(shí)候才會(huì)進(jìn)行更新,并且這個(gè)函數(shù)可以聲明兩個(gè)參數(shù)nextPropsnextState
nextProps是父組件傳給子組件的值,nextState是數(shù)據(jù)更新之后值,這兩個(gè)值可以在這個(gè)函數(shù)中獲取到。第二步當(dāng)確認(rèn)更新數(shù)據(jù)之后componentWillUpdate將要更新數(shù)據(jù),第三步依舊是render,數(shù)據(jù)發(fā)生改變r(jià)ender重新進(jìn)行了渲染。第四步是componentDidUpdate數(shù)據(jù)更新完成。

代碼的話子組件在上一部分的基礎(chǔ)上,在this.state中定義一個(gè)初始數(shù)據(jù),render中綁定一下這個(gè)數(shù)據(jù),之后再增加一個(gè)按鈕聲明一個(gè)onClick事件去改變這個(gè)數(shù)據(jù)。這樣可以看到數(shù)據(jù)更新部分的效果,我這里把第一部分的代碼刪掉了,看著不那么亂。

import React ,{Component} from 'react'

class Smzq extends Component{
constructor(props){
super(props)
this.state={
msg:'我是一個(gè)msg數(shù)據(jù)'
}
}
//是否要更新數(shù)據(jù),如果返回true才會(huì)更新數(shù)據(jù)
shouldComponentUpdate(nextProps,nextState){
console.log('01是否要更新數(shù)據(jù)')
console.log(nextProps) //父組件傳給子組件的值,這里沒(méi)有會(huì)顯示空
console.log(nextState) //數(shù)據(jù)更新后的值
return true; //返回true,確認(rèn)更新
}
//將要更新數(shù)據(jù)的時(shí)候觸發(fā)的
componentWillUpdate(){
console.log('02組件將要更新')
}
//更新數(shù)據(jù)時(shí)候觸發(fā)的生命周期函數(shù)
componentDidUpdate(){
console.log('04組件更新完成')
}
//更新數(shù)據(jù)
setMsg(){
this.setState({
msg:'我是改變后的msg數(shù)據(jù)'
})
}
render(){
console.log('03數(shù)據(jù)渲染render')
return(
<div>
{this.state.msg}
<br/>
<hr/>
<button onClick={()=>this.setMsg()}>更新msg的數(shù)據(jù)</button>
</div>
)
}
}
export default Smzq

react的生命周期函數(shù)怎么使用

三.單獨(dú)說(shuō)一下componentWillReceiveProps,父組件中改變了props傳值時(shí)觸發(fā)的函數(shù)
這個(gè)函數(shù)也就是當(dāng)我們父組件給子組件傳值的時(shí)候改變了props的值時(shí)觸發(fā)的函數(shù),剛才在第二部分中也說(shuō)到shouldComponentUpdate這個(gè)函數(shù)可以攜帶兩個(gè)參數(shù),nextProps就是父組件傳給子組件的值
在父組件中定義一個(gè)初始title數(shù)據(jù),寫(xiě)一個(gè)按鈕聲明一個(gè)onClick事件去改變這個(gè)title

四.componentWillUnmount組件將要銷毀時(shí)的函數(shù)
在父組件中定義一個(gè)flag為true的狀態(tài)值,添加一個(gè)按鈕聲明一個(gè)onClick事件去
更改這個(gè)flag實(shí)現(xiàn)銷毀組件。

父組件代碼:

import React, { Component } from 'react';
import Smzq from './components/Smzq'

class App extends Component {
constructor(props){
super(props)
this.state={
flag:true,
title:"我是app組件的標(biāo)題"
}
}
//創(chuàng)建/銷毀組件
setFlag(){
this.setState({
flag:!this.state.flag
})
}
//改變title
setTitle(){
this.setState({
title:'我是app組件改變后的title'
})
}
  render() {
   return (
     <div className="App">
{
this.state.flag?<Smzq title={this.state.title}/>:''
}
<button onClick={()=>this.setFlag()}>掛載/銷毀生命周期函數(shù)組件</button>
<button onClick={()=>this.setTitle()}>改變app組件的title</button>
     </div>
   );
}
}
export default App;

子組件完整代碼:

import React ,{Component} from 'react'

class Smzq extends Component{
constructor(props){
super(props)
this.state={
msg:'我是一個(gè)msg數(shù)據(jù)'
}
}
//組件將要掛載時(shí)候觸發(fā)的生命周期函數(shù)
componentWillMount(){
console.log('02組件將要掛載')
}
//組件掛載完成時(shí)候觸發(fā)的生命周期函數(shù)
componentDidMount(){
//Dom操作,請(qǐng)求數(shù)據(jù)放在這個(gè)里面
console.log('04組件掛載完成')
}
//是否要更新數(shù)據(jù),如果返回true才會(huì)更新數(shù)據(jù)
shouldComponentUpdate(nextProps,nextState){
console.log('01是否要更新數(shù)據(jù)')
console.log(nextProps) //父組件傳給子組件的值,這里沒(méi)有會(huì)顯示空
console.log(nextState) //數(shù)據(jù)更新后的值
return true; //返回true,確認(rèn)更新
}
//將要更新數(shù)據(jù)的時(shí)候觸發(fā)的
componentWillUpdate(){
console.log('02組件將要更新')
}
//更新數(shù)據(jù)時(shí)候觸發(fā)的生命周期函數(shù)
componentDidUpdate(){
console.log('04組件更新完成')
}
//你在父組件里面改變props傳值的時(shí)候觸發(fā)的函數(shù)
componentWillReceiveProps(){
console.log('父子組件傳值,父組件里面改變了props的值觸發(fā)的方法')
}
setMsg(){
this.setState({
msg:'我是改變后的msg數(shù)據(jù)'
})
}
//組件將要銷毀的時(shí)候觸發(fā)的生命周期函數(shù),用在組件銷毀的時(shí)候執(zhí)行操作
componentWillUnmount(){
console.log('組件銷毀了')
}
render(){
console.log('03數(shù)據(jù)渲染render')
return(
<div>
生命周期函數(shù)演示--{this.state.msg}--{this.props.title}
<br/>
<hr/>
<button onClick={()=>this.setMsg()}>更新msg的數(shù)據(jù)</button>
</div>
)
}
}
export default Smzq

點(diǎn)擊掛載/銷毀生命周期函數(shù)組件這個(gè)按鈕的時(shí)候
子組件消失,控制臺(tái)打印:組件銷毀了。

當(dāng)父組件給子組件傳值時(shí)
react的生命周期函數(shù)怎么使用

這里nextProps這個(gè)就有上圖劃紅線的值了。
那么我們?cè)冱c(diǎn)擊改變app組件的title這個(gè)按鈕

react的生命周期函數(shù)怎么使用

這里可以看到componentWillReceiveProps這個(gè)函數(shù)被觸發(fā)了,nextProps這個(gè)值也發(fā)生了改變。

到此,關(guān)于“react的生命周期函數(shù)怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

分享標(biāo)題:react的生命周期函數(shù)怎么使用
本文URL:http://jinyejixie.com/article2/ggsiic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站建設(shè)定制開(kāi)發(fā)、建站公司Google、品牌網(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)

網(wǎng)站托管運(yùn)營(yíng)
深圳市| 徐汇区| 台南县| 蓬安县| 克拉玛依市| 卓尼县| 凉城县| 河北省| 合水县| 合水县| 内黄县| 隆化县| 类乌齐县| 蓬安县| 建湖县| 霍州市| 桑日县| 蒙山县| 莎车县| 孟津县| 嵩明县| 涿鹿县| 敦化市| 酒泉市| 股票| 清徐县| 绍兴县| 监利县| 新源县| 元氏县| 新民市| 横峰县| 云霄县| 鹤壁市| 满洲里市| 壤塘县| 岐山县| 雅安市| 桂平市| 新郑市| 琼中|