react 生命周期實例分析
本文實例講述了react 生命周期。分享給大家供大家參考,具體如下:
組件掛載:
componentWillMount(組件將要掛載到頁面)->render(組件掛載中)->componentDidMount(組件掛載完成后)
組件更新:
1、shouldComponentUpdate(render之前執(zhí)行,參數(shù)為ture時執(zhí)行render,為false時不執(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 }) } //組件將要掛載到頁面時 componentWillMount() { console.log('componentWillMount'); } //組件完成掛載后 componentDidMount() { console.log('componentDidMount'); } //組件被修改之前,參數(shù)為ture時執(zhí)行render,為false時不往后執(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(子組件在被刪除時執(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'); } //子組件被刪除時執(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'); } //子組件被刪除時執(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è)計有所幫助。
相關(guān)文章
詳解React-Router中Url參數(shù)改變頁面不刷新的解決辦法
這篇文章主要介紹了詳解React-Router中Url參數(shù)改變頁面不刷新的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05React生命周期方法之componentDidMount的使用
這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)
i18next?是一款強大的國際化框架,react-i18next?是基于?i18next?適用于?React?的框架,本文介紹了?react-i18next?的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案2023-01-01