当前位置:   article > 正文

react的生命周期_componentwillmount里的请求还是无法在render前

componentwillmount里的请求还是无法在render前

1、前言

React 组件的生命周期根据广义定义描述,可以分为挂载、渲染和卸载这几个阶段。当渲染后的组件需要更新新时,我们会重新去渲染组件,直至卸载。

因此,我们可以把 React 生命周期分成两类:

  • 当组件在挂载或卸载时;
  • 当组件接收新的数据时,即组件更新时。

2、挂载或卸载过程

2.1 组件的挂载

组件挂载是最基本的过程,这个过程主要做组件状态的初始化

  1. import React, {Component} from 'react';
  2. import './App.css';
  3. class App extends Component {
  4. //打印顺序
  5. //App constructor
  6. // App componentWillMount
  7. // app render
  8. //App componentDidMount
  9. static propTypes = {
  10. }
  11. static defaultProps = {
  12. }
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. count: 0
  17. }
  18. // 重点在这里
  19. this.handleClick = this.handleClick.bind(this);
  20. console.log('App constructor')
  21. }
  22. componentWillMount() {
  23. console.log('App componentWillMount')
  24. }
  25. render() {
  26. console.log('app render')
  27. return <a href="#" onClick={this.handleClick}>Clicked me {this.state.count} times.</a>
  28. }
  29. componentDidMount() {
  30. console.log('App componentDidMount')
  31. }
  32. handleClick(e) {
  33. e.preventDefault();
  34. this.setState({ count: this.state.count + 1 })
  35. }
  36. }
  37. export default App;

特点:

  • propTypes 和 defaultProps 分别代表 props 类型检查和默认类型。这两个属性被声明成静态属性,意味着从类外面也可以访问它们,比如可以这么访问:App.propTypes 和 App.defaultProps。
  • componentWillMount 方法会在 render 方法之前执行,而 componentDidMount 方法会在 render 方法之后执行,分别代表了渲染前后的时刻
  • 读取初始 state 和 props 以及两个组件生命周期方法componentWillMount 和 componentDidMount,这些都只会在组件初始化时运行一次。

2.2 组件的卸载

组件卸载非常简单,只有 componentWillUnmount 这一个卸载前状态

  1. export default class A extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. }
  5. scroll1(){}
  6. componentDidMount() {
  7. window.addEventListener('scroll', this.srcoll1.bind(this));
  8. }
  9. componentWillUnmount(){
  10. window.removeEventListener('scroll', this.srcoll1);
  11. }
  12. render() {
  13. };
  14. }

在 componentWillUnmount 方法中,我们常常会执行一些清理方法,如事件回收或是清除定时器。

3、 数据更新过程

更新过程指的是父组件向下传递 props 或组件自身执行 setState 方法时发生的一系列更新动作。这里我们屏蔽了初始化的生命周期方法,以便观察更新过程的生命周期:

  1. import React, {Component} from 'react';
  2. import './App.css';
  3. class App extends Component {
  4. /*在shouldComponentUpdate,里设置return true 点击handleClick时打印顺序
  5. {} {count: 1} "shouldComponentUpdate"
  6. {} {count: 1} "componentWillUpdate"
  7. app render
  8. {} {count: 0} "componentDidUpdate"*/
  9. /*在shouldComponentUpdate,里设置return false 点击handleClick时打印顺序
  10. {} {count: 1} "shouldComponentUpdate"
  11. this.state.count没有变*/
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. count: 0
  16. }
  17. this.handleClick = this.handleClick.bind(this);
  18. }
  19. render() {
  20. console.log('app render')
  21. return <a href="#" onClick={this.handleClick}>Clicked me {this.state.count} times.</a>
  22. }
  23. componentWillReceiveProps(nextProps) {
  24. console.log(nextProps,'componentWillReceiveProps')
  25. }
  26. shouldComponentUpdate(nextProps, nextState) {
  27. console.log(nextProps,nextState,'shouldComponentUpdate')
  28. return true;
  29. }
  30. componentWillUpdate(nextProps, nextState) {
  31. console.log(nextProps,nextState,'componentWillUpdate')
  32. // ...
  33. }
  34. componentDidUpdate(prevProps, prevState) {
  35. console.log(prevProps,prevState,'componentDidUpdate')
  36. // ...
  37. }
  38. handleClick(e) {
  39. e.preventDefault();
  40. this.setState({ count: this.state.count + 1 })
  41. }
  42. }
  43. export default App;

特点

  • 组件自身的 state 更新了,那么会依次执行 shouldComponentUpdate、componentWillUpdate 、render 和 componentDidUpdate
  • shouldComponentUpdate 是一个特别的方法,它接收需要更新的 props 和 state,让开发者增加必要的条件判断,让其在需要时更新,不需要时不更新。因此,当方法返回 false 的时候,组件不再向下执行生命周期方法
  • component-WillUpdate 方法代表在更新过程中渲染前的时刻,提供需要更新的 props 和 state。
  • componentDidUpdate代表在更新过程中渲染后的时刻 提供更新前的 props 和state。

3.1 componentWillReceiveProps 方法

如果组件是由父组件更新 props 而更新的,那么在 shouldComponentUpdate 之前会先执行componentWillReceiveProps 方法。此方法可以作为 React 在 props 传入后,渲染之前 setState 的机会。在此方法中调用 setState 是不会二次渲染的.

  1. import React, {Component} from 'react';
  2. import './App.css';
  3. class App extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.handleChange = this.handleChange.bind(this);
  7. this.state = {
  8. activeIndex: 0,
  9. };
  10. }
  11. handleChange(e) {
  12. this.setState({
  13. activeIndex: parseInt(e.target.value, 10),
  14. });
  15. }
  16. render() {
  17. return (
  18. <div>
  19. <select value={this.state.activeIndex} onChange={this.handleChange}>
  20. <option value="1">Tab 1</option>
  21. <option value="2">Tab 2</option>
  22. <option value="3">Tab 3</option>
  23. </select>
  24. <Hello defaultActiveIndex={this.state.activeIndex} className="tabs-bar"></Hello>
  25. </div>
  26. )
  27. }
  28. }
  29. class Hello extends React.Component{
  30. constructor(props){
  31. console.log('hello constructor')
  32. super(props);
  33. this.state = {
  34. activeIndex:11,
  35. };
  36. }
  37. componentWillReceiveProps(nextProps) {
  38. if ('defaultActiveIndex' in nextProps) {
  39. console.log(nextProps.defaultActiveIndex,444)
  40. this.setState({
  41. activeIndex: nextProps.defaultActiveIndex,
  42. });
  43. }
  44. }
  45. render(){
  46. return <div>{this.props.defaultActiveIndex} + {this.state.activeIndex}</div>
  47. }
  48. }
  49. export default App;

目的是让传入的 props 判断是否存在 activeIndex。如果用了 activeIndex 初始化组件,那么每次组件更新前都会去更新组件内部的 activeIndex state,达到更新组件的目的。

4、React与vue的生命周期对照

4.1 组件挂载阶段

mounted,视图渲染完毕后,在这个阶段获取数据,同componentDidMount

4.2 组件更新阶段

4.3 组件卸载阶段

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/857457
推荐阅读
相关标签
  

闽ICP备14008679号