react類標(biāo)簽的生命周期詳解
子組件中的代碼
import React from "react";
class Son extends React.Component {
// 構(gòu)造函數(shù):初始化狀態(tài)
constructor(props) {
super(props);
// 使用 props 初始化 state
this.state = {
count: props.initialCount || 0, // 從 props 傳入的初始計(jì)數(shù)
name: props.name || "Son", // 從 props 傳入的初始名稱
isshow: props.isshow || true, // 從 props 傳入的初始是否顯示
};
}
componentDidMount() {
console.log("son componentDidMount");
}
// shouldComponentUpdate - 控制是否更新組件
shouldComponentUpdate(nextProps, nextState) {
console.log("son shouldComponentUpdate");
return true; // 默認(rèn)返回 true,表示每次都更新
}
// componentDidUpdate - 更新后調(diào)用
componentDidUpdate(prevProps, prevState) {
console.log("son componentDidUpdate");
}
// componentWillUnmount - 組件卸載前調(diào)用
componentWillUnmount() {
console.log("son componentWillUnmount");
}
// 增加計(jì)數(shù)的方法
increment = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
// 減少計(jì)數(shù)的方法
decrement = () => {
this.setState((prevState) => ({
count: prevState.count - 1,
}));
};
// 渲染方法:顯示組件內(nèi)容
render() {
return (
<div>
{this.state.isshow && (
<div>
<h2>計(jì)數(shù)器</h2>
<p>當(dāng)前名字: {this.state.name}</p>
<p>當(dāng)前計(jì)數(shù): {this.state.count}</p>
<button onClick={this.increment}>增加</button>
<button onClick={this.decrement}>減少</button>
</div>
)}
</div>
);
}
}
export default Son;父組件中代碼
import React from "react";
import Son from "./son";
class fa extends React.Component {
constructor(props) {
super(props);
// 使用 props 初始化 state
this.state = {
isshow:true
};
}
componentDidMount() {
console.log("fa componentDidMount");
}
// shouldComponentUpdate - 控制是否更新組件
shouldComponentUpdate(nextProps, nextState) {
console.log("fa shouldComponentUpdate");
return true; // 默認(rèn)返回 true,表示每次都更新
}
// componentDidUpdate - 更新后調(diào)用
componentDidUpdate(prevProps, prevState) {
console.log("fa componentDidUpdate");
}
// componentWillUnmount - 組件卸載前調(diào)用
componentWillUnmount() {
console.log("fa componentWillUnmount");
}
render() {
return (
<div>
<h1>父組件</h1>
<button onClick={()=>{
this.setState({
isshow:!this.state.isshow
})
}}>顯示/隱藏</button>
{/* 傳遞 initialCount 到子組件 Counter */}
<Son initialCount={5} name={"liu"} isshow={this.state.isshow}/>
</div>
);
}
}
export default fa;掛載時(shí)候打印出來的是

當(dāng)子組件中counter進(jìn)行更新時(shí)候執(zhí)行的生命周期

當(dāng)子組件卸載時(shí)候執(zhí)行的生命周期

每個(gè)階段的生命周期

到此這篇關(guān)于react類標(biāo)簽的生命周期的文章就介紹到這了,更多相關(guān)react生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React 項(xiàng)目中動態(tài)設(shè)置環(huán)境變量
本文主要介紹了React 項(xiàng)目中動態(tài)設(shè)置環(huán)境變量,本文將介紹兩種常用的方法,使用 dotenv 庫和通過命令行參數(shù)傳遞環(huán)境變量,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
使用react+redux實(shí)現(xiàn)彈出框案例
這篇文章主要為大家詳細(xì)介紹了使用react+redux實(shí)現(xiàn)彈出框案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
詳解在React中跨組件分發(fā)狀態(tài)的三種方法
這篇文章主要介紹了詳解在React中跨組件分發(fā)狀態(tài)的三種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
一文詳解ReactNative狀態(tài)管理redux-toolkit使用
這篇文章主要為大家介紹了ReactNative狀態(tài)管理redux-toolkit使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
React Native設(shè)備信息查看調(diào)試詳解
這篇文章主要為大家介紹了React Native設(shè)備信息查看調(diào)試詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
使用React和Redux Toolkit實(shí)現(xiàn)用戶登錄功能
在React中,用戶登錄功能是一個(gè)常見的需求,為了實(shí)現(xiàn)該功能,需要對用戶輸入的用戶名和密碼進(jìn)行驗(yàn)證,并將驗(yàn)證結(jié)果保存到應(yīng)用程序狀態(tài)中,在React中,可以使用Redux Toolkit來管理應(yīng)用程序狀態(tài),從而實(shí)現(xiàn)用戶登錄功能,需要詳細(xì)了解可以參考下文2023-05-05
React?Context?變遷及背后實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了React?Context?變遷及背后實(shí)現(xiàn)原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

