react類(lèi)標(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 傳入的初始名稱(chēng)
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í)候打印出來(lái)的是

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

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

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

到此這篇關(guān)于react類(lèi)標(biāo)簽的生命周期的文章就介紹到這了,更多相關(guān)react生命周期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React Grid Layout基礎(chǔ)使用示例教程
React Grid Layout是一個(gè)用于在React應(yīng)用程序中創(chuàng)建可拖拽和可調(diào)整大小的網(wǎng)格布局的庫(kù),通過(guò)使用React Grid Layout,我們可以輕松地創(chuàng)建自適應(yīng)的網(wǎng)格布局,并實(shí)現(xiàn)拖拽和調(diào)整大小的功能,本文介紹了React Grid Layout的基礎(chǔ)使用方法,感興趣的朋友一起看看吧2024-02-02
詳解在React-Native中持久化redux數(shù)據(jù)
這篇文章主要介紹了在React-Native中持久化redux數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
React中處理表單數(shù)據(jù)實(shí)現(xiàn)方式
本文介紹了如何在React中處理表單數(shù)據(jù),包括控制組件和非控制組件的使用,通過(guò)控制組件,表單的輸入值由React組件的狀態(tài)控制;非控制組件通過(guò)ref來(lái)訪問(wèn)表單輸入的當(dāng)前值,文章還展示了如何處理多個(gè)輸入和添加驗(yàn)證和樣式,以提供更好的用戶(hù)體驗(yàn)2025-02-02
React星星評(píng)分組件的實(shí)現(xiàn)
評(píng)分插件在購(gòu)物的應(yīng)用中經(jīng)??梢钥吹玫?,但是用著別人的總是沒(méi)有自己寫(xiě)的順手,本文就使用React實(shí)現(xiàn)星星評(píng)分組件,感興趣的可以了解一下2021-06-06

