關(guān)于react 父子組件的執(zhí)行順序
react父子組件的執(zhí)行順序
react版本:17.x,在此版本中完全可以用Hooks去進行開發(fā)了,開始先講class組件,只是為了更好的幫助理解。
在開發(fā)項目的過程中,由于項目比較大,拆分組件的結(jié)構(gòu)比較復(fù)雜,會涉及到一個組件中下面嵌套了好幾級的子級組件,這里就涉及到父子組件中生命周期的執(zhí)行順序的問題;
本文主要講兩種情況,class組件和函數(shù)組件,講一下執(zhí)行常用到的生命周期的執(zhí)行順序:
1.class組件
這里涉及到一些react組件的生命周期函數(shù),需要一定的基礎(chǔ),這里就不再贅述,詳細可以去看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時生命周期的執(zhí)行順序:

這里注意一下Parent就是ClassFn這個組件。
2.函數(shù)組件 hooks無依賴的情況
//HooksFn.tsx,以下是此文件對應(yīng)的代碼
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的第二個參數(shù)有依賴對象時,依然也是先執(zhí)行的子組件對應(yīng)的Hook。
子組件的effect函數(shù)的有依賴的情況如下:
//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'}/>在控制臺的順序如下圖:

最近在做項目的時候發(fā)現(xiàn)了問題,在進行組件拆分的時候,把其中一個很重要的請求放在了父組件沒拆出來,以為會先發(fā)送這個請求,結(jié)果排到了最后發(fā)父組件的請求,我當(dāng)時就特別納悶兒,為何會這樣了,后來百度了一大堆資料,才發(fā)現(xiàn)跟父子組件的執(zhí)行順序有關(guān)系。但是我就是想讓父組件對應(yīng)的那個請求先發(fā)送怎么辦?
最后我的解決辦法是:
把這個父組件的請求方法放在useLayoutEffect這個Hook中就行了。
在實際的項目開發(fā)過程中,確實需要會出現(xiàn)一些場景,需要父組件的執(zhí)行順序在子組件前面,這是后一定要注意react中生命周期方法的執(zhí)行順序,如果是在項目中用到Hooks組件比較多的情況,可以考慮一下使用useLayoutEffect。
以上就是本次分享的全部內(nèi)容了,希望能給你工作中遇到的難題帶來幫助!也希望大家多多支持腳本之家。
相關(guān)文章
react-native 實現(xiàn)購物車滑動刪除效果的示例代碼
這篇文章主要介紹了react-native 實現(xiàn)購物車滑動刪除效果的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
React Antd中如何設(shè)置表單只輸入數(shù)字
這篇文章主要介紹了React Antd中如何設(shè)置表單只輸入數(shù)字問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
解析react?函數(shù)組件輸入卡頓問題?usecallback?react.memo
useMemo是一個react hook,我們可以使用它在組件中包裝函數(shù)??梢允褂盟鼇泶_保該函數(shù)中的值僅在依賴項之一發(fā)生變化時才重新計算,這篇文章主要介紹了react?函數(shù)組件輸入卡頓問題?usecallback?react.memo,需要的朋友可以參考下2022-07-07

