一个React组件的生命周期分为三个部分:实例化、存在期和销毁时。实例化阶段客户端渲染时,如下依次被调用getDefaultProps()getInitialState()componentWillMount()render()componentDidMount()服务端渲染getDefaultProps()getInitialState()componentWillMount()render()注意:componentDidMount()不会再服务端被渲染;getDefaultProps对于每个组件实例来讲,这个方法只会调用一次,该组件类的所有后续应用,getDefaultPops 将不会再被调用,其返回的对象可以用于设置默认的props值。var Hello = React.creatClass({ getDefaultProps: function(){ return { name: ‘pomy’, git: ‘dwqs’ } }, render: function(){ return ( <div>Hello,{this.props.name},git username is {this.props.dwqs}</div> ) }});ReactDOM.render(<Hello />, document.body);也可以在挂载组件的时候设置 props。var data = [{title: ‘Hello’}];<Hello data={data} />或者调用 setProps (一般不需要调用)来设置其 propsvar data = [{title: ‘Hello’}];var Hello = React.render(<Demo />, document.body);Hello.setProps({data:data});但只能在子组件或组件树上调用 setProps。别调用 this.setProps 或者 直接修改 this.props。将其当做只读数据。React通过 propTypes 提供了一种验证 props 的方式,propTypes 是一个配置对象,用于定义属性类型:var survey = React.createClass({ propTypes: { survey: React.PropTypes.shape({ id: React.PropTypes.number.isRequired }).isRequired, onClick: React.PropTypes.func, name: React.PropTypes.string, score: React.PropTypes.array … }, //…})或者import React, { Component } from ‘react’import PropTypes from ‘prop-types’class BetterImage extends Component{…}BetterImage.PropTypes={ src: PropTypes.string, center: PropTypes.bool, loadingImage: PropTypes.string, defaultImage: PropTypes.string, onLoad: PropTypes.func, onError: PropTypes.func, onComplete: PropTypes.func}BetterImage.defaultProps={ ….}getInitialState对于组件的每个实例来说,这个方法的调用有且只有一次,用来初始化每个实例的 state,在这个方法里,可以访问组件的 props。每一个React组件都有自己的 state,其与 props 的区别在于 state只存在组件的内部,props 在所有实例中共享。getInitialState 和 getDefaultPops 的调用是有区别的,getDefaultPops 是对于组件类来说只调用一次,后续该类的应用都不会被调用,而 getInitialState 是对于每个组件实例来讲都会调用,并且只调一次。var LikeButton = React.createClass({ //初始化State getInitialState: function() { return {liked: false}; }, handleClick: function(event) { //设置修改State this.setState({liked: !this.state.liked}); }, render: function() { var text = this.state.liked ? ’like’ : ‘haven't liked’; return ( <p onClick={this.handleClick}> You {text} this. Click to toggle. </p> ); }});ReactDOM.render( <LikeButton />, document.getElementById(’example’));每次修改 state,都会重新渲染组件,实例化后通过 state 更新组件,会依次调用下列方法:1、shouldComponentUpdate2、componentWillUpdate3、render4、componentDidUpdatecomponentWillMount在渲染前调用,在客户端也在服务端。React 官方正式发布了 v16.3 版本。在这次的更新中,除了前段时间被热烈讨论的新 Context API 之外,新引入的两个生命周期函数 getDerivedStateFromProps,getSnapshotBeforeUpdate 以及在未来 v17.0 版本中即将被移除的三个生命周期函数 componentWillMount,componentWillReceiveProps,componentWillUpdate .在这个生命周期中你会遇到一下问题:a.首屏无数据导致白屏 在 React 应用中,许多开发者为了避免第一次渲染时页面因为没有获取到异步数据导致的白屏,而将数据请求部分的代码放在了 componentWillMount 中,希望可以避免白屏并提早异步请求的发送时间。但事实上在 componentWillMount 执行后,第一次渲染就已经开始了,所以如果在 componentWillMount 执行时还没有获取到异步数据的话,页面首次渲染时也仍然会处于没有异步数据的状态。换句话说,组件在首次渲染时总是会处于没有异步数据的状态,所以不论在哪里发送数据请求,都无法直接解决这一问题。而关于提早发送数据请求,官方也鼓励将数据请求部分的代码放在组件的 constructor 中,而不是 componentWillMount。 若是为了改善用户体验曾经用过的解决方法有两个:方法一:异步请求组件,使用nprogress 添加加载动画;import React, { Component } from ‘react’import NProgress from ’nprogress’import ’nprogress/nprogress.css’import ‘./customNprogress.styl’NProgress.configure({ showSpinner: false })export default function asyncComponent(importComponent) { class AsyncComponent extends Component { state = { component: null } async componentDidMount() { NProgress.start() const { default: component } = await importComponent() NProgress.done() this.setState({ component }) } render() { const C = this.state.component return C ? <C {…this.props} /> : null } } return AsyncComponent}const AsyncNotFound = asyncComponent(() => import(/* webpackChunkName: “NotFound” */ ‘@/routes/NotFound’))方法二:使用 onreadystatechange 去监听 readyState,在资源加载完成之前加载一个只有框架的静态页面,页面不请求数据。当数据请求完成之后再将路由切换到真实的首页。 function listen () { if (document.readyState == ‘complete’) { // 资源加载完成 ReactDom.render( <Provider store={store}> <Router> <Route path="/" component={Index}/> </Router> </Provider>, document.getElementById(‘root’) ) } else { // 资源加载中 ReactDom.render( <Provider store={store}> <Router> <Route path="/" component={FirstScreen}/> </Router> </Provider>, document.getElementById(‘root’) ) }}document.onreadystatechange = listen具体参考解决React首屏加载白屏的问题b.事件订阅另一个常见的用例是在 componentWillMount 中订阅事件,并在 componentWillUnmount 中取消掉相应的事件订阅。但事实上 React 并不能够保证在 componentWillMount 被调用后,同一组件的 componentWillUnmount 也一定会被调用。一个当前版本的例子如服务端渲染时,componentWillUnmount 是不会在服务端被调用的,所以在 componentWillMount 中订阅事件就会直接导致服务端的内存泄漏。另一方面,在未来 React 开启异步渲染模式后,在 componentWillMount 被调用之后,组件的渲染也很有可能会被其他的事务所打断,导致 componentWillUnmount 不会被调用。而 **componentDidMount 就不存在这个问题,在 componentDidMount 被调用后,componentWillUnmount 一定会随后被调用到,并根据具体代码清除掉组件中存在的事件订阅。**render该方法会创建一个虚拟DOM,用来表示组件的输出。对于一个组件来讲,render方法是唯一一个必需的方法。render方法需要满足下面几点:只能通过 this.props 和 this.state 访问数据(不能修改)可以返回 null,false(这种场景下,react渲染一个<noscript>标签,当返回null或者false时,ReactDOM.findDOMNode(this)返回null) 或者任何React组件只能出现一个顶级组件,不能返回一组元素不能改变组件的状态不能修改DOM的输出render方法返回的结果并不是真正的DOM元素,而是一个虚拟的表现,类似于一个DOM tree的结构的对象。react之所以效率高,就是这个原因。render执行情况如下: 1. 首次加载 2. setState改变组件内部state。 注意: 此处是说通过setState方法改变。 3. 接受到新的props注意:因为数据是异步的情况,会导致组件重复渲染componentDidMount该方法不会在服务端被渲染的过程中调用。该方法被调用时,已经渲染出真实的 DOM,可以再该方法中通过 this.getDOMNode() 访问到真实的 DOM(推荐使用 ReactDOM.findDOMNode())。var data = [..];var comp = React.createClass({ render: function(){ return <imput .. /> }, componentDidMount: function(){ $(this.getDOMNode()).autoComplete({ src: data }) }})由于组件并不是真实的 DOM 节点,而是存在于内存之中的一种数据结构,叫做虚拟 DOM (virtual DOM)。只有当它插入文档以后,才会变成真实的 DOM 。有时需要从组件获取真实 DOM 的节点,这时就要用到 ref 属性:var Area = React.createClass({ render: function(){ this.getDOMNode(); //render调用时,组件未挂载,这里将报错 return <canvas ref=‘mainCanvas’> }, componentDidMount: function(){ var canvas = this.refs.mainCanvas.getDOMNode(); //这是有效的,可以访问到 Canvas 节点 }})需要注意的是,由于 this.refs.[refName] 属性获取的是真实 DOM ,所以必须等到虚拟 DOM 插入文档以后,才能使用这个属性,否则会报错。如果ref回调函数以inline函数的方式来指定,那么在组件更新的时候ref回调会被调用2次。第一次回调的时候传入的参数是null,而第二次的时候才真正的传入DOM节点更多了解ref使用从React官方文档看 refs 的使用和未来获取真实dom,并获取dom css 三种方法存在期此时组件已经渲染好并且用户可以与它进行交互,比如鼠标点击,手指点按,或者其它的一些事件,导致应用状态的改变,你将会看到下面的方法依次被调用;componentWillReceiveProps()shouldComponentUpdate()componentWillUpdate()render()componentDidUpdate()componentWillReceiveProps当props发生变化时执行,初始化render时不执行,在这个回调函数里面,你可以根据属性的变化,通过调用this.setState()来更新你的组件状态,旧的属性还是可以通过this.props来获取,这里调用更新状态是安全的,并不会触发额外的render调用。componentWillReceiveProps: function(nextProps){ if(nextProps.checked !== undefined){ this.setState({ checked: nextProps.checked }) }}了解更多点击此处shouldComponentUpdateshouldComponentUpdate函数是重渲染时render()函数调用前被调用的函数,它接受两个参数:nextProps和nextState,分别表示下一个props和下一个state的值。并且,当函数返回false时候,阻止接下来的render()函数及后面的 componentWillUpdate,componentDidUpdate 方法的调用,阻止组件重渲染,而返回true时,组件照常重渲染。了解更多点击此处–真的讲的好componentWillUpdate这个方法和 componentWillMount 类似,在组件接收到了新的 props 或者 state 即将进行重新渲染前,componentWillUpdate(object nextProps, object nextState) 会被调用,注意不要在此方面里再去更新 props 或者 state。componentDidUpdate这个方法和 componentDidMount 类似,在组件重新被渲染之后,componentDidUpdate(object prevProps, object prevState) 会被调用。可以在这里访问并修改 DOM。销毁componentWillUnmount每当React使用完一个组件,这个组件必须从 DOM 中卸载后被销毁,此时 componentWillUnmout 会被执行,完成所有的清理和销毁工作,在 componentDidMount 中添加的任务都需要再该方法中撤销,如创建的定时器或事件监听器。当再次装载组件时,以下方法会被依次调用:1、getInitialState2、componentWillMount3、render4、componentDidMountReact v.16生命周期constructor(props) // 初始化参数componentWillMount()render() // 第一次渲染 componentDidMount()当父组件向子组件传入props发生改变后,依次调用componentWillReceiveProps(nextProps)shouldComponentUpdate(nextProps, nextState) componentWillUpdate()render() //子组件更新渲染componentDidUpdate()当组件自身state发生变化后componentWillUpdate()render() //组件再次更新渲染componentDidUpdate()当组件卸载componentWillUnmount()与低于React16版本的比较React16新的生命周期弃用了componentWillMount、componentWillReceiveProps,componentWillUpdate新增了getDerivedStateFromProps、getSnapshotBeforeUpdate来代替弃用的三个钩子函数(componentWillMount、componentWillReceivePorps,componentWillUpdate)React16并没有删除这三个钩子函数,但是不能和新增的钩子函数(getDerivedStateFromProps、getSnapshotBeforeUpdate)混用,React17将会删除componentWillMount、componentWillReceivePorps,componentWillUpdate新增了对错误的处理(componentDidCatch)相关文章那个生命周期方法更适合请求数据react服务端渲染来谈谈Reactv16.3新生命周期知识点及遇到的问题React16版生命周期