React入门 10:React 生命周期

本篇内容主要是 React 的生命周期(包括版本更新后的API变化),先用原生JS的示例代码展示生命周期,再展示React的生命周期,
在官网中 State and Lifecycle 可查看 API 的详情。

image.png

如图,React生命周期主要包括三个阶段:初始化阶段、运行中阶段和销毁阶段,在React不同的生命周期里,会依次触发不同的钩子函数。

1. 原生JS 里的生命周期

示例代码:

let app = document.querySelector('#app')

//create div
let div = document.createElement('div')

let state = 0

//componentWillMount()
//render 

div.innerHTML = `
  <p>${state}</p>
  <button>+1</button>
  <button>die</button>
`

//mount div
app.appendChild(div)

div.querySelector('button').onclick = ()=>{
  //update == render
  state += 1
  div.querySelector('p').innerText = state
}

//destory div
div.querySelectorAll('button')[1].onclick = ()=>{
  div.querySelector('button').onclick = null
  div.querySelectorAll('button')[1].onclick = null
  div.remove()
  div = null
}

上面例子中的 div 有4个动作:生成、挂载、更新、死亡。

2. React 里的生命周期

函数是没有生命周期的,需用到 class

class App extends React.Component {
  constructor() {
    super();
    // 1. 加载默认状态
    this.state = {
      amount: 100,
    };
    console.log('1.constructor:创建App')
  }

  componentWillMount() {
    console.log('2. App组件将要挂载');
  }

  add() {
    console.log("点击了App的 +1 按钮")
    this.setState({
      amount: this.state.amount + 1
    });
  }

  render() {
    console.log('3. render:App组件挂载渲染')
    return(
      <div className="App">
        <div>{this.props.n}</div>
        <span>amount: {this.state.amount}</span>
        <button onClick={() => this.add()}>+1</button>
      </div>
    );
  }

  componentDidMount() {
    console.log('4. App组件挂载之后')
  }

  // 到此,App组件的「初始化阶段」结束,进入「进行时阶段」
  
  //5. 这一步可以做性能优化点,判断是否真的要更新。
  shouldComponentUpdate(nextPros, nextState) {
    console.log('5. 判断是否要更新')
    if (this.state.n === nextState.n) {
      return true;
    } else {
      return false;
    }
  }
  
  componentWillUpdate() {
    console.log('6. App组件将要更新啦!');
  }

  // 7. 重新render

  componentDidUpdate() {
    console.log('8. App组件更新完毕!')
  }

  // 9. 如果我们先操作UnMount
  componentWillUnmount() {
    // 在「Parent」组件中点击「go die」按钮,会销毁「App组件」
    console.log('9. App组件即将销毁');
  }

  // 10. 当App组件从Parent组件收到的Props改变,执行。
  componentWillReceiveProps() {
    console.log('10. App组件接受的Props改变了')
  }

  // 11. 再次进入 第5步的「componentWillUpdate」

}

class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      hasChild: true,
      n: 0
    }
    console.log('创建Parent')
  }

  add() {
    console.log('点击了Parent的+1按钮')
    this.setState({
      n: this.state.n + 1
    });
  }
  
  removeChild() {
    // 消灭App组件
    this.setState({
      hasChild: false
    });
  }

  render() {
    return(
      <div className="parent">
        <span>n: {this.state.n}</span> 
        <button onClick={() => this.add()}>+1</button>
        <button onClick = {() => this.removeChild()}>go die</button>
        {
          this.state.hasChild ? <App n={this.state.n}/> : null
        }
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);

代码在线地址

3. 版本更新了

在即将到来的 React17.0 版本,React 团队对生命周期做了调整,将会移除 componentWillMountcomponentWillReceivePropscomponentWillUpdate 这三个生命周期,因为这些生命周期方法容易被误解和滥用。

3.1 过时的生命周期函数

这些函数仍然有效,但不建议在新代码中使用它们。
componentWillMountcomponentWillReceivePropscomponentWillUpdate 这三个生命周期,更改名称为:

  • UNSAFE_componentWillMount()
  • UNSAFE_componentWillReceiveProps()
  • UNSAFE_componentWillUpdate

中文文档地址

3.2 新增的生命周期函数

  • getDerivedStateFromProps
    当组件实例化的时候,这个方法替代了 componentWillMount(),而当接收到新的 props 时,该方法替代了 componentWillReceiveProps()componentWillUpdate()触发时机: 会在每次组件被重新渲染前被调用, 这意味着无论是父组件的更新, props 的变化, 或是组件内部执行了 setState(), 它都会被调用.

注意: componentWillReceivePropsgetDerivedStateFromProps 同时存在,控制台会报错。

getDerivedStateFromProps 会在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。

  • getSnapshotBeforeUpdate
    这函数会在 render 之后执行,而执行之时DOM元素还没有被更新,给了一个机会去获取DOM信息,计算得到一个 snapshot,这个snapshot 会作为 componentDidUpdate 的第三个参数传入。

这个函数应该大部分开发者都用不上(潜台词就是:不要用!)

3.3 自动更名

If you don’t have the time to migrate or test these components, we recommend running a “codemod” script that renames them automatically:

cd your_project
npx react-codemod rename-unsafe-lifecycles

4. 总结

挂载

当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下:

更新

当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下:

用一个静态函数 getDerivedStateFromProps 来取代被deprecate的几个生命周期函数,就是强制开发者在 render 之前只做无副作用的操作,而且能做的操作局限在根据 propsstate 决定新的 state,而已。

卸载

当组件从 DOM 中移除时会调用如下方法:

来个图吧

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335