解读React源码(三):生命周期的管理艺术
前言React的主要思想是通过构建可复用组件来构建页面. class Collections extends Component { constructor(props) { super(props); this.state = { a: 1 } } add = () => { this.setState({ a: this.state.a + 1 }); } componentWillMount() { console.log('componentWillMount'); } componentDidMount() { console.log('componentDidMount'); this.setState({ a: 2 }); } componentWillUnMount() { console.log('componentWillUnMount'); } render() { console.log('render'); return ( <div className="collections"> { this.state.a } <Example a={ this.state.a } /> <button onClick={ this.add }>btn</button> </div> ); } } import React,{ Component,PropTypes } from 'react'; export default class Example extends Component { static propTypes = { a: PropTypes.number.isRequired }; static defaultProps = { a: 0 }; constructor(props) { super(props); this.state = { b: 1 }; } add = () => { this.setState({ b: this.state.b + 1 }); } componentWillReceiveProps(nextProps) { console.log(nextProps); if (nextProps.a === 4) { this.setState({ b: 1000 }); } } shouldComponentUpdate(nextProps,nextState) { console.log(nextProps); console.log(nextState); if (nextState.b === 4) { return false; } return true; } componentWillMount() { console.log('子组件componentWillMount'); } componentDidMount() { console.log('子组件componentDidMount'); } render() { console.log('子组件render'); return ( <div> <p>a:{ this.props.a }</p> <p>b:{ this.state.b }</p> <button onClick={ this.add }>子组件add</button> </div> ); } } 初探react生命周期当首次挂载组件时,按顺序执行getDefaultProps,getInitialState,componentWillMount,render,componentDidMount 使用createClass创建自定义组件createClass是创建自定义组件的入口方法,负责管理生命周期方法中的getDefaultProps. var ReactClass = { // 创建自定义组件 createClass: function(spec) { var Constructor = function(props,context,updater) { // /自动绑定 if (this._reactAutoBindPairs.length) { bindAutoBindMethods(this); } this.props = props; this.context = context; this.refs = emptyObject; this.updater = updater || ReactNoopUpdateQueue; this.state = null; // ReactClass没有构造函数,通过getInitialState和componentWillMount来代替 var initialState = this.getInitialState ? this.getInitialState() : null; this.state = initialState; }; // 原型继承父类 Constructor.prototype = new ReactClassComponent(); Constructor.prototype.constructor = constructor; Constructor.prototype._reactAutoBindPairs = []; // 合并mixin injectedMixins.forEach( minSpecIntoComponent.bind(null,Constructor) ); minSpecIntoComponent(Constructor,spec); // 所有mixin合并后初始化defaultProps(在整个生命周期中,getDefaultProps只执行一次) if (Constructor.getDefaultProps) { Constructor.defaultProps = Constructor.getDefaultProps(); } // 减少查找并设置原型的时间 for (var methodName in ReactClassInterface) { if (!Constructor.prototype[methodName]) { Constructor.prototype[methodName] = null; } } return Constructor; } }; MOUNTINGmountComponent负责管理生命周期中的getInitialState,render和componentDidMount RECEIVE_PROPSupdateComponent负责管理生命周期中的componentWillReceiveProps,componentWillUpdate, componentWillReceiveProps(nextProps) { console.log(nextProps); if (nextProps.a === 4) { this.setState({ b: 1000 },() => { console.log(this.state.b); }); } } UNMOUTINGunmountComponnet负责管理生命周期中的componentWillUnMount 无状态组件无状态组件只是一个render方法,并没有组件类的实例化过程,也没有实例返回. import React from 'react'; const HelloWorld = (props) => { return ( <div>{ props.a }</div> ); } export default HelloWorld; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |