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

React中父子組件生命周期的執(zhí)行順序

 更新時間:2026年06月10日 09:08:50   作者:卷簾依舊  
在React中,父子組件的生命周期執(zhí)行順序遵循一個清晰的規(guī)律,父組件先準備,子組件再掛載;子組件先卸載,父組件再清理,?下面就來詳細的介紹一下

在 React 中,父子組件的生命周期執(zhí)行順序遵循一個清晰的規(guī)律:父組件先準備,子組件再掛載;子組件先卸載,父組件再清理。

核心原則:渲染階段由父到子(render 遞歸),提交階段(DOM 更新)再由子到父(componentDidMount/Update 遞歸)。

一、初始掛載階段(Mounting)

順序:父組件 getDerivedStateFromProps / render → 子組件生命周期 → 父組件 componentDidMount

詳細步驟(以類組件為例):

  1. 父組件:constructor
  2. 父組件:getDerivedStateFromProps
  3. 父組件:render
    → 此時解析出子組件實例,開始子組件的生命周期
  4. 子組件:constructor
  5. 子組件:getDerivedStateFromProps
  6. 子組件:render
  7. 子組件:componentDidMount
  8. 父組件:componentDidMount

注意:componentDidMount 是子組件先執(zhí)行,父組件后執(zhí)行。

二、更新階段(Updating)

觸發(fā)條件:props 或 state 改變。

2.1 由父組件 state 變化引起的更新

  1. 父組件:getDerivedStateFromProps
  2. 父組件:shouldComponentUpdate
  3. 父組件:render
    → 重新生成子組件,觸發(fā)子組件更新
  4. 子組件:getDerivedStateFromProps
  5. 子組件:shouldComponentUpdate
  6. 子組件:render
  7. 子組件:getSnapshotBeforeUpdate
  8. 子組件:componentDidUpdate
  9. 父組件:getSnapshotBeforeUpdate
  10. 父組件:componentDidUpdate

2.2 由子組件自身 state 變化引起的更新

只會觸發(fā)子組件自身的更新生命周期,父組件不受影響(除非子組件通過回調修改父組件 state)。

三、卸載階段(Unmounting)

順序:父組件先標記卸載 → 子組件 componentWillUnmount → 父組件 componentWillUnmount

  1. 父組件:決定卸載子組件(例如條件渲染移除子組件)
  2. 子組件:componentWillUnmount
  3. 父組件:componentWillUnmount

子組件先執(zhí)行清理工作,父組件最后清理。

四、函數(shù)組件中的近似順序(useEffect)

函數(shù)組件沒有直接等價的生命周期,但 useEffect 的執(zhí)行順序模擬了部分行為:

階段類組件順序函數(shù)組件近似順序
掛載父 render → 子 render → 子 didMount → 父 didMount父 render → 子 render → 子 useLayoutEffect → 父 useLayoutEffect → 子 useEffect → 父 useEffect
更新父 render → 子 render → 子 didUpdate → 父 didUpdate類似,但 cleanup 在上一次 effect 之前執(zhí)行
卸載子 willUnmount → 父 willUnmount子 useEffect cleanup → 父 useEffect cleanup

注意:useEffect 是在瀏覽器完成布局與繪制之后異步執(zhí)行,所以 useEffect 中父子順序仍然是子先于父(因為組件樹是深度優(yōu)先渲染的)。

五、關鍵總結

  • 掛載:父 render 先完成,但父 componentDidMount 要等所有子組件掛載完才執(zhí)行。
  • 更新:父 render 先執(zhí)行,然后子組件更新,最后父 componentDidUpdate。
  • 卸載:子組件先執(zhí)行 componentWillUnmount,父組件最后執(zhí)行。
  • 函數(shù)組件:useEffect 的順序是子先于父,但執(zhí)行時間點在布局/繪制之后。

這個順序保證了:

  • 子組件在父組件訪問它們之前已經準備好(或已清理)
  • DOM 測量/滾動位置等操作可以在合適的時機進行

實例

以下是一個當前時間的實例,每秒更新:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }
  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }
  componentWillUnmount() {
    clearInterval(this.timerID);
  }
  tick() {
    this.setState({
      date: new Date()
    });
  }
  render() {
    return (
      <div>
        <h1>Hello, Runoob!</h1>
        <h2>現(xiàn)在時間是:{this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}
const root = ReactDOM.createRoot(document.body);
root.render(
  <Clock />
);

以下實例在 Hello 組件加載以后,通過 componentDidMount 方法設置一個定時器,每隔100毫秒重新設置組件的透明度,并重新渲染:

class Hello extends React.Component {
  constructor(props) {
      super(props);
      this.state = {opacity: 1.0};
  }
  componentDidMount() {
    this.timer = setInterval(function () {
      var opacity = this.state.opacity;
      opacity -= .05;
      if (opacity < 0.1) {
        opacity = 1.0;
      }
      this.setState({
        opacity: opacity
      });
    }.bind(this), 100);
  }
  render () {
    return (
      <div style={{opacity: this.state.opacity}}>
        Hello {this.props.name}
      </div>
    );
  }
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <Hello name="world"/>
);

以下實例初始化 state , setNewnumber 用于更新 state。所有生命周期在 Content 組件中。

class Button extends React.Component {
  constructor(props) {
    super(props);
    this.state = { data: 0 };
    this.setNewNumber = this.setNewNumber.bind(this);
  }
  setNewNumber() {
    this.setState({ data: this.state.data + 1 });
  }
  render() {
    return (
      <div>
        <button onClick={this.setNewNumber}>INCREMENT</button>
        <Content myNumber={this.state.data} />
      </div>
    );
  }
}
class Content extends React.Component {
  componentDidMount() {
    console.log("Component DID MOUNT!");
  }
  shouldComponentUpdate(newProps, newState) {
    return true;
  }
  componentDidUpdate(prevProps, prevState) {
    console.log("Component DID UPDATE!");
  }
  componentWillUnmount() {
    console.log("Component WILL UNMOUNT!");
  }
  render() {
    return (
      <div>
        <h3>{this.props.myNumber}</h3>
      </div>
    );
  }
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <div>
    <Button />
  </div>
);

到此這篇關于React中父子組件生命周期的執(zhí)行順序的文章就介紹到這了,更多相關React 父子組件執(zhí)行順序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • react中定義變量并使用方式

    react中定義變量并使用方式

    這篇文章主要介紹了react中定義變量并使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • ReactNative?狀態(tài)管理redux使用詳解

    ReactNative?狀態(tài)管理redux使用詳解

    這篇文章主要介紹了ReactNative?狀態(tài)管理redux使用詳解
    2023-03-03
  • React組件與事件的創(chuàng)建使用教程

    React組件與事件的創(chuàng)建使用教程

    react事件綁定時。this并不會指向當前DOM元素。往往使用bind來改變this指向,今天通過本文給大家介紹React事件綁定的方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-02-02
  • React?高德地圖進京證路線規(guī)劃問題記錄(匯總)

    React?高德地圖進京證路線規(guī)劃問題記錄(匯總)

    這篇文章主要介紹了React高德地圖進京證路線規(guī)劃問題小記,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • react項目自行配置熱更新的實現(xiàn)

    react項目自行配置熱更新的實現(xiàn)

    本文主要介紹了react項目自行配置熱更新的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-11-11
  • 詳解前端路由實現(xiàn)與react-router使用姿勢

    詳解前端路由實現(xiàn)與react-router使用姿勢

    本篇文章主要介紹了詳解前端路由和react-router使用姿勢,詳細的介紹了react-router的用法,有興趣的可以了解一下
    2017-08-08
  • React優(yōu)化子組件render的使用

    React優(yōu)化子組件render的使用

    這篇文章主要介紹了React優(yōu)化子組件render的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Rust中Trait的使用

    Rust中Trait的使用

    在Rust中,Trait是一個核心概念,它允許我們定義類型應該具有的行為,本文就來具體介紹一下Rust中Trait的使用,感興趣的可以了解一下,感興趣可以了解一下
    2024-03-03
  • 使用react和redux構建一個簡單的計數(shù)器

    使用react和redux構建一個簡單的計數(shù)器

    這篇文章主要為大家詳細介紹了如何使用react和redux構建一個簡單的計數(shù)器,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • React Native實現(xiàn)地址挑選器功能

    React Native實現(xiàn)地址挑選器功能

    這篇文章主要為大家詳細介紹了React Native仿地址挑選器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論

陆河县| 保德县| 徐水县| 临沂市| 达拉特旗| 日土县| 平罗县| 寻乌县| 常熟市| 西林县| 延吉市| 壤塘县| 刚察县| 河曲县| 阿拉尔市| 芜湖市| 滁州市| 固始县| 湘潭市| 吴江市| 吉木乃县| 安仁县| 明溪县| 余姚市| 榆树市| 三穗县| 青川县| 无极县| 紫金县| 玉田县| 南京市| 阿尔山市| 千阳县| 耒阳市| 卓尼县| 威海市| 昌都县| 穆棱市| 乐都县| 乌鲁木齐县| 东兴市|