欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ù)不會(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 (
    <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ù)不會(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 (
    <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;

希望本文所述對(duì)大家react程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • react實(shí)現(xiàn)阻止父容器滾動(dòng)

    react實(shí)現(xiàn)阻止父容器滾動(dòng)

    這篇文章主要介紹了react實(shí)現(xiàn)阻止父容器滾動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • react實(shí)現(xiàn)消息顯示器

    react實(shí)現(xiàn)消息顯示器

    這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)消息顯示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳解React-Router中Url參數(shù)改變頁面不刷新的解決辦法

    詳解React-Router中Url參數(shù)改變頁面不刷新的解決辦法

    這篇文章主要介紹了詳解React-Router中Url參數(shù)改變頁面不刷新的解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • React中多語言的配置方式

    React中多語言的配置方式

    這篇文章主要介紹了React中多語言的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • React生命周期方法之componentDidMount的使用

    React生命周期方法之componentDidMount的使用

    這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • React如何避免子組件無效刷新

    React如何避免子組件無效刷新

    這篇文章主要介紹了React幾種避免子組件無效刷新的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Javascript之提高React性能的技巧

    Javascript之提高React性能的技巧

    一些剛開始學(xué)習(xí) React,或者從其他框架轉(zhuǎn)入 React 的開發(fā)者,一開始可能不會(huì)太關(guān)注性能。因?yàn)樾枰恍r(shí)間來發(fā)現(xiàn)新學(xué)習(xí)的框架的性能缺點(diǎn)。這篇文章主要介紹提高React性能的技巧,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • react使用websocket實(shí)時(shí)通信方式

    react使用websocket實(shí)時(shí)通信方式

    這篇文章主要介紹了react使用websocket實(shí)時(shí)通信方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React?中使用?react-i18next?國(guó)際化的過程(react-i18next?的基本用法)

    React?中使用?react-i18next?國(guó)際化的過程(react-i18next?的基本用法)

    i18next?是一款強(qiáng)大的國(guó)際化框架,react-i18next?是基于?i18next?適用于?React?的框架,本文介紹了?react-i18next?的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案
    2023-01-01
  • react 父組件與子組件之間的值傳遞的方法

    react 父組件與子組件之間的值傳遞的方法

    本篇文章主要介紹了react 父組件與子組件之間的值傳遞的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09

最新評(píng)論