最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
什么是react生命周期

小編這次要給大家分享的是什么是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 (
      
      
{this.input=input}} value={this.state.inputValue} onChange={this.change.bind(this)}/>
    { this.state.list.map((v,i)=>{ return( ); }) }
); } } export default Test;

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 (
    
  • {this.props.name}{this.props.content}
  • ); } 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ù)不會(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 (
        
  • {this.props.name}{this.props.content}
  • ); } 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;

    看完這篇關(guān)于什么是react生命周期的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。


    新聞標(biāo)題:什么是react生命周期
    URL標(biāo)題:http://fisionsoft.com.cn/article/pshhoh.html