React 组件生命周期详解
创作时间:
作者:
@小白创作中心
React 组件生命周期详解
引用
1
来源
1.
https://www.runoob.com/react/react-component-life-cycle.html
React组件生命周期是React开发中的核心概念之一,它描述了组件从创建到销毁的整个过程。本文将详细介绍React组件的生命周期,并通过实例代码帮助读者更好地理解这些生命周期方法的使用场景。
在本章节中我们将讨论 React 组件的生命周期。
组件的生命周期可分成三个状态:
- Mounting(挂载):已插入真实 DOM
- Updating(更新):正在被重新渲染
- Unmounting(卸载):已移出真实 DOM
挂载
当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下:
render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。
这些方法的详细说明,可以参考官方文档。
更新
每当组件的 state 或 props 发生变化时,组件就会更新。
当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下:
render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。
这些方法的详细说明,可以参考官方文档。
卸载
当组件从 DOM 中移除时会调用如下方法:
- componentWillUnmount()
: 在组件卸载及销毁之前直接调用。
这些方法的详细说明,可以参考官方文档。
实例
以下是一个当前时间的实例,每秒更新:
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
componentDidMount() {
this.timerID = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({ date: new Date() });
}
render() {
return (
<div>
<h1>Hello, Runoob!</h1>
<h2>现在时间是:{this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
const root = ReactDOM.createRoot(document.body);
root.render(<Clock />);
以下实例在 Hello 组件加载以后,通过 componentDidMount 方法设置一个定时器,每隔100毫秒重新设置组件的透明度,并重新渲染:
class Hello extends React.Component {
constructor(props) {
super(props);
this.state = { opacity: 1.0 };
}
componentDidMount() {
this.timer = setInterval(function () {
var opacity = this.state.opacity;
opacity -= .05;
if (opacity < 0.1) {
opacity = 1.0;
}
this.setState({ opacity: opacity });
}.bind(this), 100);
}
render() {
return (
<div style={{ opacity: this.state.opacity }}>
Hello {this.props.name}
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<Hello name="world" />);
以下实例初始化state,setNewnumber用于更新state。所有生命周期在Content组件中。
class Button extends React.Component {
constructor(props) {
super(props);
this.state = { data: 0 };
this.setNewNumber = this.setNewNumber.bind(this);
}
setNewNumber() {
this.setState({ data: this.state.data + 1 });
}
render() {
return (
<div>
<button onClick={this.setNewNumber}>INCREMENT</button>
<Content myNumber={this.state.data} />
</div>
);
}
}
class Content extends React.Component {
componentDidMount() {
console.log("Component DID MOUNT!");
}
shouldComponentUpdate(newProps, newState) {
return true;
}
componentDidUpdate(prevProps, prevState) {
console.log("Component DID UPDATE!");
}
componentWillUnmount() {
console.log("Component WILL UNMOUNT!");
}
render() {
return (
<div>
<h3>{this.props.myNumber}</h3>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<div>
<Button />
</div>
);
热门推荐
离婚房产分割遵循哪些原则
离婚时房屋财产分割指南:从购房情况到法律依据
慈菇怎么洗和处理 吃慈菇有什么好处
十部冷门古典小说:被时光尘封的文学宝藏
昆明适合长期居住吗?海拔1891米的昆明,长期居住需要注意什么?
船舶“9米干舷标志”的由来及相关要求
签订农村土地承包合同应约定哪些内容?
变帅气变漂亮,要读《面部按摩》有效的护肤指南
创业公司如何建立独特的企业文化和价值观?
热门的经典游戏角色盘点:2025年高人气手游推荐
瘙痒是什么导致的?为什么身体会发痒?
嘴巴上长大包怎么办
2025年3月电影盛宴:从科幻喜剧到经典重映,哪部影片将触动你的心弦?
基本医疗保险主要保障什么
甲状腺结节穿刺活检:适用人群、手术过程及结果解读
2024年无锡市重点产业规划布局全景图谱
混凝土预制件的应用范围详解
盘点舌肌萎缩的背后不可忽视的病因
狗狗能吃木瓜吗?宠物健康新知揭秘!
肌少症并非老年人“专利”
揭秘京剧魅力:生动说唱脸谱经典歌词全解析
低功耗 DC/DC 转换器的复杂设计原理分析介绍
肝性脑病的表现分哪5期
新能源车动能回收技术全景解析:从特斯拉到保时捷的功率对比和影响
电机额定电流测试方法,详解电机额定电流的测试步骤
2025年购车国补新变:国四燃油车纳入补贴范围
诵读·地名背后山西史 | 灵石:地名因祥瑞而来
土壤成分一般的检测方法
有效的提问,请教问题是有讲究的
扁桃体发炎化脓的四种治疗方法