最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react 生命周期實(shí)例分析

 更新時(shí)間:2020年05月18日 09:21:42   作者:人生如初見_張默  
這篇文章主要介紹了react 生命周期,結(jié)合實(shí)例形式分析了react 生命周期基本原理、操作步驟與注意事項(xiàng),需要的朋友可以參考下

本文實(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問題解決方法

    本篇文章主要介紹了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)航的示例

    本篇文章主要介紹了react-native使用react-navigation進(jìn)行頁面跳轉(zhuǎn)導(dǎo)航的示例,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)

    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源碼的方法

    這篇文章主要介紹了使用VScode 插件debugger for chrome 調(diào)試react源碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • React傳遞參數(shù)的幾種方式

    React傳遞參數(shù)的幾種方式

    本文詳細(xì)的介紹了React傳遞參數(shù)的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • React事件處理和表單的綁定詳解

    React事件處理和表單的綁定詳解

    這篇文章主要介紹了React事件處理和表單的綁定,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理

    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-native-fs插件的使用以及遇到的坑

    詳解react-native-fs插件的使用以及遇到的坑

    本篇文章主要介紹了react-native-fs插件的使用以及遇到的坑,詳細(xì)的介紹了react-native-fs安裝使用,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • react路由基礎(chǔ)解讀(Router、Link和Route)

    react路由基礎(chǔ)解讀(Router、Link和Route)

    這篇文章主要介紹了react路由基礎(chǔ)解讀(Router、Link和Route),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • React實(shí)現(xiàn)二級聯(lián)動(左右聯(lián)動)

    React實(shí)現(xiàn)二級聯(lián)動(左右聯(lián)動)

    這篇文章主要為大家詳細(xì)介紹了React實(shí)現(xiàn)二級聯(lián)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論

乌鲁木齐县| 宁安市| 吕梁市| 永修县| 子长县| 连山| 敖汉旗| 游戏| 论坛| 紫阳县| 贵阳市| 九龙坡区| 咸阳市| 轮台县| 泰宁县| 西乌珠穆沁旗| 永嘉县| 天台县| 吐鲁番市| 大厂| 荔波县| 福清市| 海淀区| 黄梅县| 上饶县| 北流市| 南昌市| 凉山| 虞城县| 大港区| 崇明县| 收藏| 崇义县| 昌都县| 横峰县| 年辖:市辖区| 大关县| 康马县| 普格县| 凌源市| 昌邑市|