react 生命周期實(shí)例分析
本文實(shí)例講述了react 生命周期。分享給大家供大家參考,具體如下:
組件掛載:
componentWillMount(組件將要掛載到頁面)->render(組件掛載中)->componentDidMount(組件掛載完成后)
組件更新:
1、shouldComponentUpdate(render之前執(zhí)行,參數(shù)為ture時(shí)執(zhí)行render,為false時(shí)不執(zhí)行render)
componentWillUpdate(shouldComponentUpdate之后執(zhí)行)
componentDidUpdate(render之后執(zhí)行)
順序:shouldComponentUpdate-》componentWillUpdate-》render-》componentDidUpdate
import React, { Component, Fragment } from 'react';
import List from './List.js';
class Test extends Component {
constructor(props) {
super(props);
this.state={
inputValue:'aaa',
list:['睡覺','打游戲'],
}
// this.add=this.add.bind(this);
}
addList() {
this.setState({
list:[...this.state.list,this.state.inputValue],
inputValue:''
})
}
change(e) {
this.setState({
// inputValue:e.target.value
inputValue:this.input.value
})
}
delete(i) {
// console.log(i);
const list = this.state.list;
list.splice(i,1);
this.setState({
list:list
})
}
//組件將要掛載到頁面時(shí)
componentWillMount() {
console.log('componentWillMount');
}
//組件完成掛載后
componentDidMount() {
console.log('componentDidMount');
}
//組件被修改之前,參數(shù)為ture時(shí)執(zhí)行render,為false時(shí)不往后執(zhí)行
shouldComponentUpdate() {
console.log('1-shouldComponentUpdate');
return true;
}
//shouldComponentUpdate之后
componentWillUpdate() {
console.log('2-componentWillUpdate');
}
//render執(zhí)行之后
componentDidUpdate() {
console.log('4-componentDidUpdate');
}
//組件掛載中
render() {
console.log('3-render');
return (
<Fragment>
<div>
<input ref={(input)=>{this.input=input}} value={this.state.inputValue} onChange={this.change.bind(this)}/>
<button onClick={this.addList.bind(this)}>添加</button>
</div>
<ul>
{
this.state.list.map((v,i)=>{
return(
<List key={i} content={v} index={i} delete={this.delete.bind(this)} />
);
})
}
</ul>
</Fragment>
);
}
}
export default Test;
2、componentWillReceiveProps(子組件中執(zhí)行。組件第一次存在于虛擬dom中,函數(shù)不會被執(zhí)行,如果已經(jīng)存在于dom中,函數(shù)才會執(zhí)行)
componentWillUnmount(子組件在被刪除時(shí)執(zhí)行)
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class List extends Component {
constructor(props) {
super(props);
this.delete = this.delete.bind(this);
}
//組件第一次存在于虛擬dom中,函數(shù)不會被執(zhí)行
//如果已經(jīng)存在于dom中,函數(shù)才會執(zhí)行
componentWillReceiveProps() {
console.log('componentWillReceiveProps');
}
//子組件被刪除時(shí)執(zhí)行
componentWillUnmount() {
console.log('componentWillUnmount');
}
render() {
return (
<li
onClick={this.delete}>{this.props.name}{this.props.content}</li>
);
}
delete=() => {
this.props.delete(this.props.index);
}
}
List.propTypes={
name:PropTypes.string.isRequired,
content:PropTypes.string,
index:PropTypes.number,
delete:PropTypes.func
}
//設(shè)置默認(rèn)值:
List.defaultProps={
name:'喜歡'
}
export default List;
組件性能優(yōu)化:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class List extends Component {
constructor(props) {
super(props);
this.delete = this.delete.bind(this);
}
//組件第一次存在于虛擬dom中,函數(shù)不會被執(zhí)行
//如果已經(jīng)存在于dom中,函數(shù)才會執(zhí)行
componentWillReceiveProps() {
console.log('componentWillReceiveProps');
}
//子組件被刪除時(shí)執(zhí)行
componentWillUnmount() {
console.log('componentWillUnmount');
}
shouldComponentUpdate(nextProps,nextState) {
if (nextProps.content !== this.props.content) {
return true;
} else {
return false;
}
}
render() {
return (
<li
onClick={this.delete}>{this.props.name}{this.props.content}</li>
);
}
delete=() => {
this.props.delete(this.props.index);
}
}
List.propTypes={
name:PropTypes.string.isRequired,
content:PropTypes.string,
index:PropTypes.number,
delete:PropTypes.func
}
//設(shè)置默認(rèn)值:
List.defaultProps={
name:'喜歡'
}
export default List;
希望本文所述對大家react程序設(shè)計(jì)有所幫助。
相關(guān)文章
react-router browserHistory刷新頁面404問題解決方法
本篇文章主要介紹了react-router browserHistory刷新頁面404問題解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12
react-native使用react-navigation進(jìn)行頁面跳轉(zhuǎn)導(dǎo)航的示例
本篇文章主要介紹了react-native使用react-navigation進(jìn)行頁面跳轉(zhuǎn)導(dǎo)航的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)
基本路由跳轉(zhuǎn)是最常見的一種方式,下面介紹React 中常用的幾種路由跳轉(zhuǎn)方式,感興趣的朋友一起看看吧2023-12-12
使用VScode 插件debugger for chrome 調(diào)試react源碼的方法
這篇文章主要介紹了使用VScode 插件debugger for chrome 調(diào)試react源碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理
這篇文章主要介紹了40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
react路由基礎(chǔ)解讀(Router、Link和Route)
這篇文章主要介紹了react路由基礎(chǔ)解讀(Router、Link和Route),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
React實(shí)現(xiàn)二級聯(lián)動(左右聯(lián)動)
這篇文章主要為大家詳細(xì)介紹了React實(shí)現(xiàn)二級聯(lián)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

