日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

關于react?父子組件的執行順序_React

作者:ohMyGod_123 ? 更新時間: 2022-11-07 編程語言

react父子組件的執行順序

react版本:17.x,在此版本中完全可以用Hooks去進行開發了,開始先講class組件,只是為了更好的幫助理解。

在開發項目的過程中,由于項目比較大,拆分組件的結構比較復雜,會涉及到一個組件中下面嵌套了好幾級的子級組件,這里就涉及到父子組件中生命周期的執行順序的問題;

本文主要講兩種情況,class組件和函數組件,講一下執行常用到的生命周期的執行順序:

1.class組件?

這里涉及到一些react組件的生命周期函數,需要一定的基礎,這里就不再贅述,詳細可以去看react官方文檔,請看代碼:

import React from 'react';
 
const buildClass = (name)=>{
  return class extends React.Component{
    constructor(props) {
      super(props);
      console.log( name + ' constructor');
    }
    UNSAFE_componentWillMount() {
      console.log( name + ' componentWillMount');
    }
    componentDidMount() {
      console.log( name + ' componentDidMount');
    }
    componentWillUnmount() {
      console.log( name + ' componentWillUnmount');
    }
    UNSAFE_componentWillReceiveProps(nextProps) {
      console.log( name + ' componentWillReceiveProps(nextProps)');
    }
    shouldComponentUpdate(nextProps, nextState) {
      console.log( name + ' shouldComponentUpdate(nextProps, nextState)');
      return true;
    }
    UNSAFE_componentWillUpdate(nextProps, nextState) {
      console.log( name + ' componentWillUpdate(nextProps, nextState)');
    }
    componentDidUpdate(prevProps, prevState) {
      console.log( name + ' componetDidUpdate(prevProps, prevState)');
    }
  }
}
class Child extends buildClass('Child'){
  render(){
    console.log('Child render')
    return (
      <div>child</div>
    )
  }
}
class ClassFn extends buildClass('Parent'){
  render(){
    console.log('Parent render')
    return (
      <Child />
    )
  }
};
 
export default ClassFn;

然后在其他組件中去引用ClassFn.tsx這個文件;可以看一下頁面在初始化載入ClassFn時生命周期的執行順序:

這里注意一下Parent就是ClassFn這個組件。

2.函數組件? hooks無依賴的情況

//HooksFn.tsx,以下是此文件對應的代碼
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
 
const Ai = props => {
  useEffect(() => {
    console.log("ai組件加載完成");
  });
  return <div className="ai" />;
};
 
const Home = props => {
  useEffect(() => {
    console.log("Home組件加載完成");
  });
  return (
    <div className="home">
      <Ai />
    </div>
  );
};
 
function HooksFn() {
  useEffect(() => {
    console.log("HooksFn組件加載完成");
  });
 
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Home />
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
 
export default HooksFn;

在上面的代碼中,注意一下控制臺打印出來的順序是:

ai組件加載完成 => Home組件加載完成 => HooksFn組件加載完成

如下圖所示:

此時如果effect的第二個參數有依賴對象時,依然也是先執行的子組件對應的Hook。

子組件的effect函數的有依賴的情況如下:

//HooksFn.tsx
import React, { useEffect } from "react";
  
const Ai = props => {
  useEffect(() => {
    console.log("ai組件加載完成");
  }, [props.name]);
  return <div className="ai" />;
};
 
const Home = props => {
  useEffect(() => {
    console.log("Home組件加載完成");
  }, [props.name]);
  return (
    <div className="home">
      <Ai {...props}/>
    </div>
  );
};
 
function HooksFn(props) {
  // Hooks中有依賴的情況
  useEffect(() => {
    console.log("HooksFn組件加載完成");
  }, [props.name]);
 
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Home {...props}/>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
 
export default HooksFn;

在其他地方飲用水HooksFn這個組件:

import HooksFn from '@/pages/exercise/HooksFn';
 
<HooksFn name={'12'}/>

在控制臺的順序如下圖:

最近在做項目的時候發現了問題,在進行組件拆分的時候,把其中一個很重要的請求放在了父組件沒拆出來,以為會先發送這個請求,結果排到了最后發父組件的請求,我當時就特別納悶兒,為何會這樣了,后來百度了一大堆資料,才發現跟父子組件的執行順序有關系。但是我就是想讓父組件對應的那個請求先發送怎么辦?

最后我的解決辦法是:

把這個父組件的請求方法放在useLayoutEffect這個Hook中就行了。

在實際的項目開發過程中,確實需要會出現一些場景,需要父組件的執行順序在子組件前面,這是后一定要注意react中生命周期方法的執行順序,如果是在項目中用到Hooks組件比較多的情況,可以考慮一下使用useLayoutEffect。

原文鏈接:https://blog.csdn.net/qq_35770417/article/details/120706178

欄目分類
最近更新