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>
);
热门推荐
肇庆到凤凰古城自驾游攻略:三条路线详解及凤凰古城游玩攻略
狗狗也能养生?红枣泡水了解一下!
秋日古诗里的花语密码
各国送花禁忌大揭秘:小心踩雷!
番红花:神秘之美的花语揭秘
国庆档必看!《忠诚》与《独孤里桥之役》再现朝鲜战场英雄传奇
黄岛油库大火:应急救援的那些事儿
黄岛油库大火35周年:一场雷击引发的特大火灾
八一渔八、粤艺馆、八十里的蛇年春节美食大赏
上海两大主题乐园春节活动全攻略:欢乐谷VS蓝精灵乐园
扬州到安徽自驾游亲子路线大揭秘!
喝粥、吃面都不养胃!只有这6招才管用
古诗词里的10大爱情典故,流转千年,美到无瑕
2025 年春节:传统与现代交融的新景象
唐伯虎的山水画:诗意与情感的完美融合
《落花诗册》估值百亿,揭秘唐伯虎书法艺术
唐伯虎:才子与画家的传奇人生
《桃花庵歌》爆红网络:一首500年前的诗,为何打动了当代人?
从《桃花庵歌》看唐伯虎的“佛系人生”
五台山vs平遥古城:从文化到美食的深度对比
抗美援朝70周年:邱少云精神永流传
邱少云精神如何影响当代青年?
邯郸古村落:历史与现代的碰撞与融合
七步沟:太行深处的自然与人文瑰宝
数字赋能千年古城:邯郸文旅的创新之路
抗美援朝70周年:邱少云的英雄事迹再回顾
重庆邮电大学移通学院师生赴邱少云纪念馆开展爱国主义教育活动
大爱无疆:社会各界为西藏地震灾区捐款捐物
五一打卡安徽五日游,黄山美景不容错过!
屯溪老街:千年商埠展新颜,徽州文化活化石