網站首頁 編程語言 正文
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
相關推薦
- 2023-04-22 python統計函數被調用次數的實現_python
- 2022-10-19 react編寫可編輯標題示例詳解_React
- 2022-04-09 Python中變量的作用域詳解_python
- 2022-07-04 教你在容器中使用nginx搭建上傳下載的文件服務器_nginx
- 2022-07-10 如何替換重構依賴里面的Service
- 2021-12-01 Linux五步構建內核樹_Linux
- 2022-08-21 Android用Canvas繪制貝塞爾曲線_Android
- 2023-03-15 Docker網絡配置及部署SpringCloud項目詳解_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支