在本章节中我们将讨论 React 组件的生命周期。
组件的生命周期可分成三个状态:
- Mounting(挂载):已插入真实 DOM
- Updating(更新):正在被重新渲染
- Unmounting(卸载):已移出真实 DOM
挂载
当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下:
constructor(): 在 React 组件挂载之前,会调用它的构造函数。getDerivedStateFromProps(): 在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。render(): render() 方法是 class 组件中唯一必须实现的方法。componentDidMount(): 在组件挂载后(插入 DOM 树中)立即调用。
render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。
这些方法的详细说明,可以参考官方文档。
更新
每当组件的 state 或 props 发生变化时,组件就会更新。
当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下:
getDerivedStateFromProps(): 在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。根据 shouldComponentUpdate() 的返回值,判断 React 组件的输出是否受当前 state 或 props 更改的影响。shouldComponentUpdate():当 props 或 state 发生变化时,shouldComponentUpdate() 会在渲染执行之前被调用。render(): render() 方法是 class 组件中唯一必须实现的方法。getSnapshotBeforeUpdate(): 在最近一次渲染输出(提交到 DOM 节点)之前调用。componentDidUpdate(): 在更新后会被立即调用。
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毫秒重新设置组件的透明度,并重新渲染:
React 实例
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组件中。
React 实例
https://avg.163.com/topic/detail/9240621
https://avg.163.com/topic/detail/9240665
https://avg.163.com/topic/detail/9240712
https://avg.163.com/topic/detail/9240752
https://avg.163.com/topic/detail/9240539
https://avg.163.com/topic/detail/9240546
https://avg.163.com/topic/detail/9240552
https://avg.163.com/topic/detail/9240606
https://avg.163.com/topic/detail/9240582
https://avg.163.com/topic/detail/9240593
https://avg.163.com/topic/detail/9240576
https://avg.163.com/topic/detail/9240632
https://avg.163.com/topic/detail/9240646
https://avg.163.com/topic/detail/9240620
https://avg.163.com/topic/detail/9240537
https://avg.163.com/topic/detail/9240585
https://avg.163.com/topic/detail/9240642
https://avg.163.com/topic/detail/9240674
https://avg.163.com/topic/detail/9240678
https://avg.163.com/topic/detail/9240662
https://avg.163.com/topic/detail/9240564
https://avg.163.com/topic/detail/9240559
https://avg.163.com/topic/detail/9240630
https://avg.163.com/topic/detail/9240691
https://avg.163.com/topic/detail/9240731
https://avg.163.com/topic/detail/9240719
https://avg.163.com/topic/detail/9240740
https://avg.163.com/topic/detail/9240707
https://avg.163.com/topic/detail/9240622
https://avg.163.com/topic/detail/9240608
https://avg.163.com/topic/detail/9240673
https://avg.163.com/topic/detail/9240717
https://avg.163.com/topic/detail/9240766
https://avg.163.com/topic/detail/9240797
https://avg.163.com/topic/detail/9240774
https://avg.163.com/topic/detail/9240554
https://avg.163.com/topic/detail/9240758
https://avg.163.com/topic/detail/9240553
https://avg.163.com/topic/detail/9240650
https://avg.163.com/topic/detail/9240692
https://avg.163.com/topic/detail/9240742
https://avg.163.com/topic/detail/9240663
https://avg.163.com/topic/detail/9240709
https://avg.163.com/topic/detail/9240830
https://avg.163.com/topic/detail/9240805
https://avg.163.com/topic/detail/9240603
https://avg.163.com/topic/detail/9240788
https://avg.163.com/topic/detail/9240598
https://avg.163.com/topic/detail/9240634
https://avg.163.com/topic/detail/9240735
https://avg.163.com/topic/detail/9240777
https://avg.163.com/topic/detail/9240810
https://avg.163.com/topic/detail/9240745
https://avg.163.com/topic/detail/9240866
https://avg.163.com/topic/detail/9240834
https://avg.163.com/topic/detail/9240640
https://avg.163.com/topic/detail/9240833
https://avg.163.com/topic/detail/9240859
https://avg.163.com/topic/detail/9240680
https://avg.163.com/topic/detail/9240767
https://avg.163.com/topic/detail/9240798
https://avg.163.com/topic/detail/9240838
https://avg.163.com/topic/detail/9240780
https://avg.163.com/topic/detail/9240896
https://avg.163.com/topic/detail/9240863
https://avg.163.com/topic/detail/9240675
https://avg.163.com/topic/detail/9240722
https://avg.163.com/topic/detail/9240885
https://avg.163.com/topic/detail/9240721
https://avg.163.com/topic/detail/9240759
https://avg.163.com/topic/detail/9240839
https://avg.163.com/topic/detail/9240867
https://avg.163.com/topic/detail/9240815
https://avg.163.com/topic/detail/9240920
https://avg.163.com/topic/detail/9240542
https://avg.163.com/topic/detail/9240790
https://avg.163.com/topic/detail/9240871
https://avg.163.com/topic/detail/9240892
https://avg.163.com/topic/detail/9240846
https://avg.163.com/topic/detail/9240947
https://avg.163.com/topic/detail/9240590
https://avg.163.com/topic/detail/9240569
https://avg.163.com/topic/detail/9240772
https://avg.163.com/topic/detail/9240924
https://avg.163.com/topic/detail/9240547
https://avg.163.com/topic/detail/9240879
https://avg.163.com/topic/detail/9240979
https://avg.163.com/topic/detail/9240644
https://avg.163.com/topic/detail/9240604
https://avg.163.com/topic/detail/9240577
https://avg.163.com/topic/detail/9240915
https://avg.163.com/topic/detail/9240561
https://avg.163.com/topic/detail/9240654
https://avg.163.com/topic/detail/9240908
https://avg.163.com/topic/detail/9241004
https://avg.163.com/topic/detail/9240677
https://avg.163.com/topic/detail/9240651
https://avg.163.com/topic/detail/9240617
https://avg.163.com/topic/detail/9240941
https://avg.163.com/topic/detail/9240601
https://avg.163.com/topic/detail/9240706
https://avg.163.com/topic/detail/9240937
https://avg.163.com/topic/detail/9241027
https://avg.163.com/topic/detail/9240763
https://avg.163.com/topic/detail/9240690
https://avg.163.com/topic/detail/9240661
https://avg.163.com/topic/detail/9240968
https://avg.163.com/topic/detail/9240645
https://avg.163.com/topic/detail/9240741
https://avg.163.com/topic/detail/9240563
https://avg.163.com/topic/detail/9240898
https://avg.163.com/topic/detail/9240803
https://avg.163.com/topic/detail/9240949
https://avg.163.com/topic/detail/9240562
https://avg.163.com/topic/detail/9240705
https://avg.163.com/topic/detail/9240992
https://avg.163.com/topic/detail/9240683
https://avg.163.com/topic/detail/9240819
https://avg.163.com/topic/detail/9240607
https://avg.163.com/topic/detail/9240922
https://avg.163.com/topic/detail/9240829
https://avg.163.com/topic/detail/9240974
https://avg.163.com/topic/detail/9240605
https://avg.163.com/topic/detail/9240746
https://avg.163.com/topic/detail/9241022
https://avg.163.com/topic/detail/9240744
https://avg.163.com/topic/detail/9240851
https://avg.163.com/topic/detail/9240639
https://avg.163.com/topic/detail/9240952
https://avg.163.com/topic/detail/9240858
https://avg.163.com/topic/detail/9240999
https://avg.163.com/topic/detail/9240649
https://avg.163.com/topic/detail/9240785
https://avg.163.com/topic/detail/9240558
https://avg.163.com/topic/detail/9240966
https://avg.163.com/topic/detail/9241058
https://avg.163.com/topic/detail/9240795
https://avg.163.com/topic/detail/9240726
https://avg.163.com/topic/detail/9240769
https://avg.163.com/topic/detail/9240890
https://avg.163.com/topic/detail/9240921
https://avg.163.com/topic/detail/9240822
https://avg.163.com/topic/detail/9241045
https://avg.163.com/topic/detail/9240782
https://avg.163.com/topic/detail/9240888
https://avg.163.com/topic/detail/9240679
https://avg.163.com/topic/detail/9240977
https://avg.163.com/topic/detail/9240882
https://avg.163.com/topic/detail/9241031
https://avg.163.com/topic/detail/9240693
https://avg.163.com/topic/detail/9240566
https://avg.163.com/topic/detail/9240861
https://avg.163.com/topic/detail/9241067
https://avg.163.com/topic/detail/9240814
https://avg.163.com/topic/detail/9240914
https://avg.163.com/topic/detail/9240720
https://avg.163.com/topic/detail/9241002
https://avg.163.com/topic/detail/9241026
https://avg.163.com/topic/detail/9240946
https://avg.163.com/topic/detail/9240820
https://avg.163.com/topic/detail/9240586
https://avg.163.com/topic/detail/9240990
https://avg.163.com/topic/detail/9240844
https://avg.163.com/topic/detail/9240942
https://avg.163.com/topic/detail/9240760
https://avg.163.com/topic/detail/9240792
https://avg.163.com/topic/detail/9241052
https://avg.163.com/topic/detail/9240971
https://avg.163.com/topic/detail/9240852
https://avg.163.com/topic/detail/9240672
https://avg.163.com/topic/detail/9241014
https://avg.163.com/topic/detail/9240876
https://avg.163.com/topic/detail/9240965
https://avg.163.com/topic/detail/9240989
https://avg.163.com/topic/detail/9240824
https://avg.163.com/topic/detail/9240541
https://avg.163.com/topic/detail/9240609
https://avg.163.com/topic/detail/9240883
https://avg.163.com/topic/detail/9240716
https://avg.163.com/topic/detail/9240574
https://avg.163.com/topic/detail/9240615
https://avg.163.com/topic/detail/9240808
https://avg.163.com/topic/detail/9241059
https://avg.163.com/topic/detail/9240727
https://avg.163.com/topic/detail/9240768
https://avg.163.com/topic/detail/9240997
https://avg.163.com/topic/detail/9240910
https://avg.163.com/topic/detail/9240764
https://avg.163.com/topic/detail/9240796
https://avg.163.com/topic/detail/9240657
https://avg.163.com/topic/detail/9240841
https://avg.163.com/topic/detail/9240853
https://avg.163.com/topic/detail/9240594
https://avg.163.com/topic/detail/9240647
https://avg.163.com/topic/detail/9240823
https://avg.163.com/topic/detail/9241050
https://avg.163.com/topic/detail/9240906
https://avg.163.com/topic/detail/9240934
https://avg.163.com/topic/detail/9241015
https://avg.163.com/topic/detail/9241047
https://avg.163.com/topic/detail/9240568
https://avg.163.com/topic/detail/9240635
https://avg.163.com/topic/detail/9240703
https://avg.163.com/topic/detail/9240855
https://avg.163.com/topic/detail/9240891
https://avg.163.com/topic/detail/9240831
https://avg.163.com/topic/detail/9240696
https://avg.163.com/topic/detail/9240873
https://avg.163.com/topic/detail/9240895
https://avg.163.com/topic/detail/9240611
https://avg.163.com/topic/detail/9240676
https://avg.163.com/topic/detail/9240549
https://avg.163.com/topic/detail/9240592
https://avg.163.com/topic/detail/9240641
https://avg.163.com/topic/detail/9240701
https://avg.163.com/topic/detail/9240962
https://avg.163.com/topic/detail/9240991
https://avg.163.com/topic/detail/9240799
https://avg.163.com/topic/detail/9240560
https://avg.163.com/topic/detail/9240656
https://avg.163.com/topic/detail/9240761
https://avg.163.com/topic/detail/9240807
https://avg.163.com/topic/detail/9240837
https://avg.163.com/topic/detail/9240860
https://avg.163.com/topic/detail/9240887
https://avg.163.com/topic/detail/9240575
https://avg.163.com/topic/detail/9240902
https://avg.163.com/topic/detail/9240917
https://avg.163.com/topic/detail/9241018
https://avg.163.com/topic/detail/9240939
https://avg.163.com/topic/detail/9240743
https://avg.163.com/topic/detail/9240781
https://avg.163.com/topic/detail/9240869
https://avg.163.com/topic/detail/9240897
https://avg.163.com/topic/detail/9240912
https://avg.163.com/topic/detail/9240623
https://avg.163.com/topic/detail/9240927
https://avg.163.com/topic/detail/9240944
https://avg.163.com/topic/detail/9241049
https://avg.163.com/topic/detail/9240548
https://avg.163.com/topic/detail/9240616
https://avg.163.com/topic/detail/9240929
https://avg.163.com/topic/detail/9240959
https://avg.163.com/topic/detail/9240925
https://avg.163.com/topic/detail/9241024
https://avg.163.com/topic/detail/9240832
https://avg.163.com/topic/detail/9240588
https://avg.163.com/topic/detail/9240698
https://avg.163.com/topic/detail/9240970
https://avg.163.com/topic/detail/9240993
https://avg.163.com/topic/detail/9240812
https://avg.163.com/topic/detail/9240571
https://avg.163.com/topic/detail/9240984
https://avg.163.com/topic/detail/9240954
https://avg.163.com/topic/detail/9241053
https://avg.163.com/topic/detail/9240868
https://avg.163.com/topic/detail/9240628
https://avg.163.com/topic/detail/9240733
https://avg.163.com/topic/detail/9240660
https://avg.163.com/topic/detail/9240704
https://avg.163.com/topic/detail/9240737
https://avg.163.com/topic/detail/9240945
https://avg.163.com/topic/detail/9240544
https://avg.163.com/topic/detail/9240978
https://avg.163.com/topic/detail/9240969
https://avg.163.com/topic/detail/9240994
https://avg.163.com/topic/detail/9241016
https://avg.163.com/topic/detail/9241019
https://avg.163.com/topic/detail/9240540
https://avg.163.com/topic/detail/9240739
https://avg.163.com/topic/detail/9240778
https://avg.163.com/topic/detail/9240972
https://avg.163.com/topic/detail/9240596
https://avg.163.com/topic/detail/9241008
https://avg.163.com/topic/detail/9241033
https://avg.163.com/topic/detail/9241061
https://avg.163.com/topic/detail/9240551
https://avg.163.com/topic/detail/9240597
https://avg.163.com/topic/detail/9240840
https://avg.163.com/topic/detail/9240610
https://avg.163.com/topic/detail/9241009
https://avg.163.com/topic/detail/9240666
https://avg.163.com/topic/detail/9240953
https://avg.163.com/topic/detail/9240901
https://avg.163.com/topic/detail/9240686
https://avg.163.com/topic/detail/9240734
https://avg.163.com/topic/detail/9240770
https://avg.163.com/topic/detail/9240648
https://avg.163.com/topic/detail/9240872
https://avg.163.com/topic/detail/9240655
https://avg.163.com/topic/detail/9240996
https://avg.163.com/topic/detail/9240637
https://avg.163.com/topic/detail/9240684
https://avg.163.com/topic/detail/9240771
https://avg.163.com/topic/detail/9240802
https://avg.163.com/topic/detail/9240801
https://avg.163.com/topic/detail/9240681
https://avg.163.com/topic/detail/9240900
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> );