React生命周期函数

矫情吗;* 2021-09-21 23:14 638阅读 0赞

相关函数












































方法

调用时机

constructor(props, context)

构造函数,在创建组件的时候调用一次。

void componentWillMount()

在组件挂载之前调用一次。如果在这个函数里面调用setState,本次的render函数可以看到更新后的state,并且只渲染一次。

void componentDidMount()

在组件挂载之后调用一次。这个时候,子组件也都挂载好了,可以在这里使用refs。

void componentWillReceiveProps(nextProps)

props是父组件传递给子组件的。父组件发生render的时候子组件就会调用componentWillReceiveProps(不管props有没有更新,也不管父子组件之间有没有数据交换)。

bool shouldComponentUpdate(nextProps, nextState)

组件挂载之后,每次调用setState后都会调用shouldComponentUpdate判断是否需要重新渲染组件。默认返回true,需要重新render。在比较复杂的应用里,有一些数据的改变并不影响界面展示,可以在这里做判断,优化渲染效率。

void componentWillUpdate(nextProps, nextState)

shouldComponentUpdate返回true或者调用forceUpdate之后,componentWillUpdate会被调用。

void componentDidUpdate()

除了首次render之后调用componentDidMount,其它render结束之后都是调用componentDidUpdate。

ReactElement render()

render是一个React组件所必不可少的核心函数(上面的其它函数都不是必须的)。记住,不要在render里面修改state。

void componentWillUnmount()

组件被卸载的时候调用。一般在componentDidMount里面注册的事件需要在这里删除。

componentWillMount、componentDidMount和componentWillUpdate、componentDidUpdate可以对应起来。区别在于,前者只有在挂载的时候会被调用;而后者在以后的每次更新渲染之后都会被调用。

更新方式

在react中,触发render的有4条路径。

以下假设shouldComponentUpdate都是按照默认返回true的方式。

  1. 首次渲染Initial Render
  2. 调用this.setState (并不是一次setState会触发一次render,React可能会合并操作,再一次性进行render)
  3. 父组件发生更新(一般就是props发生改变,但是就算props没有改变或者父子组件之间没有数据交换也会触发render)
  4. 调用this.forceUpdate

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3h1ZW1lbmdydWkxMg_size_16_color_FFFFFF_t_70

componentWillMount与componentDidMount对比

1、componentWillMount 将要装载,在render之前调用;

  1. componentDidMount,(装载完成),在render之后调用

2、componentWillMount 每一个组件render之前立即调用;

  1. componentDidMount render之后并不会立即调用,而是所有的子组件都render完之后才可以调用

3、componentWillMount 可以在服务端被调用,也可以在浏览器端被调用;

  1. componentDidMount 只能在浏览器端被调用,在服务器端使用react的时候不会被调用

注意,如果在shouldComponentUpdate里面返回false可以提前退出更新路径。

componentDidUpdate与componentWillReceiveProps对比

1、componentWillReceiveProps在组件接受新的props之前触发;

componentDidUpdate在组件接受新的props之后触发

2、componentWillReceiveProps更新状态是同步的

componentDidUpdate更新状态是异步的

这点区别非常重要,也是componentWillReceiveProps生命周期被废弃的重要原因(可能导致某些问题), 所以推荐使用componentDidUpdate

React组件生命周期的测试

  1. class LifeCycle extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. alert("LifeCycle constructor");
  5. this.state = {str: "hello"};
  6. }
  7. componentWillMount() {
  8. alert("componentWillMount");
  9. }
  10. componentDidMount() {
  11. alert("componentDidMount");
  12. }
  13. componentWillReceiveProps(nextProps) {
  14. alert("componentWillReceiveProps");
  15. alert(nextProps.num);
  16. }
  17. shouldComponentUpdate(nextProps, nextState) {
  18. alert("shouldComponentUpdate");
  19. return true; // 记得要返回true
  20. }
  21. componentWillUpdate(nextProps, nextState) {
  22. alert("componentWillUpdate");
  23. }
  24. componentDidUpdate() {
  25. alert("componentDidUpdate");
  26. }
  27. componentWillUnmount() {
  28. alert("componentWillUnmount");
  29. }
  30. setTheState() {
  31. let s = "hello";
  32. if (this.state.str === s) {
  33. s = "HELLO";
  34. }
  35. this.setState({
  36. str: s
  37. });
  38. }
  39. forceItUpdate() {
  40. this.forceUpdate();
  41. }
  42. render() {
  43. alert("render()");
  44. return (
  45. <div>
  46. <span>{"Props:"}<h2>{parseInt(this.props.num)}</h2></span>
  47. <br/>
  48. <span>{"State:"}<h2>{this.state.str}</h2></span>
  49. </div>
  50. );
  51. }
  52. }
  53. class Container extends React.Component {
  54. constructor(props) {
  55. super(props);
  56. this.state = {
  57. num: Math.random() * 100
  58. };
  59. }
  60. propsChange() {
  61. this.setState({
  62. num: Math.random() * 100
  63. });
  64. }
  65. setLifeCycleState() {
  66. this.refs.rLifeCycle.setTheState();
  67. }
  68. forceLifeCycleUpdate() {
  69. this.refs.rLifeCycle.forceItUpdate();
  70. }
  71. unmountLifeCycle() {
  72. // 这里卸载父组件也会导致卸载子组件
  73. ReactDOM.unmountComponentAtNode(document.getElementById("root"));
  74. }
  75. parentForceUpdate() {
  76. this.forceUpdate();
  77. }
  78. render() {
  79. return (
  80. <div>
  81. <button className="propsChange"
  82. onClick={this.propsChange.bind(this)}>propsChange</button>
  83. <button className="setState"
  84. onClick={this.setLifeCycleState.bind(this)}>setState</button>
  85. <button className="forceUpdate"
  86. onClick={this.forceLifeCycleUpdate.bind(this)}>forceUpdate</button>
  87. <button className="unmount"
  88. onClick={this.unmountLifeCycle.bind(this)}>unmount</button>
  89. <button butto className="parentForceUpdateWithoutChange"
  90. onClick={this.parentForceUpdate.bind(this)}>parentForceUpdateWithoutChange</button>
  91. <LifeCycle ref="rLifeCycle" num={this.state.num}></LifeCycle>
  92. </div>
  93. );
  94. }
  95. }
  96. ReactDOM.render(
  97. <Container></Container>,
  98. document.getElementById('root')
  99. );

componentWillMount 和 componentDidMount 那个更适合请求数据?

componentWillMount

这个方法正确调用的时候是在component第一次render之前, 所以第一眼看上去觉得就应该在这里去fetch datas.

但是这里有个问题, 在异步请求数据中这一次返回的是空数据, 因为在’render’之前不会返回数据。所以在渲染的时候没有办法得到数据,也不能在componentWillMount中返回一个Promise(因为Promise的特性之一就是状态不可变),或者用setTimeout也是不适合的。正确的处理方式就不要在这里请求数据,而是让组件的状态在这里正确的初始化.

顺便说一句在es6中,使用extend component的方式里的constructor函数和componentWillMount是通用的作用,所以你在构造函数里初始化了组件的状态就不必在WillMount做重复的事情了.

componentDidMount

componentDidMount是在render之后调用一次,component已经初始化完成了。

在生产时,componentDidMount生命周期函数是最好的时间去请求数据,其中最重要的原因就是componentDidMount一定是在组件初始化完成之后才会请求数据,因此不会报什么警告或者错误,我们正常请求数据完成之后一般都会setState.

参考:

发表评论

表情:
评论列表 (有 0 条评论,638人围观)

还没有评论,来说两句吧...

相关阅读