新聞中心
小編這次要給大家分享的是什么是react生命周期,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
永平網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),永平網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為永平成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的永平做網(wǎng)站的公司定做!
組件掛載:
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 (); } } export default Test; {this.input=input}} value={this.state.inputValue} onChange={this.change.bind(this)}/>{ this.state.list.map((v,i)=>{ return(
); }) }
2、componentWillReceiveProps(子組件中執(zhí)行。組件第一次存在于虛擬dom中,函數(shù)不會(huì)被執(zhí)行,如果已經(jīng)存在于dom中,函數(shù)才會(huì)執(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ù)不會(huì)被執(zhí)行 //如果已經(jīng)存在于dom中,函數(shù)才會(huì)執(zhí)行 componentWillReceiveProps() { console.log('componentWillReceiveProps'); } //子組件被刪除時(shí)執(zhí)行 componentWillUnmount() { console.log('componentWillUnmount'); } render() { return (
組件性能優(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ù)不會(huì)被執(zhí)行 //如果已經(jīng)存在于dom中,函數(shù)才會(huì)執(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 (
看完這篇關(guān)于什么是react生命周期的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。
新聞標(biāo)題:什么是react生命周期
URL標(biāo)題:http://fisionsoft.com.cn/article/pshhoh.html