ReactTransitionGroup模块通过ReactWithAddons模块提供外部接口,还被ReactCSSTransitionGroup模块调用;用于操控子节点componentWillAppear等方法的执行时机,以及由上层组件提供的childFactory方法劫持渲染子节点。
'use strict'; var _assign = require('object-assign'); // 构造函数作为普通函数调用报错 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } // 继承 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var React = require('./React'); // getChildMapping方法将props.children扁平化处理 // mergeChildMappings方法将前后两次已经扁平化的数据props.children复合为一 var ReactTransitionChildMapping = require('./ReactTransitionChildMapping'); var emptyFunction = require('fbjs/lib/emptyFunction'); var ReactTransitionGroup = function (_React$Component) { _inherits(ReactTransitionGroup, _React$Component); function ReactTransitionGroup() { var _temp, _this, _ret; _classCallCheck(this, ReactTransitionGroup); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = ( _this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), // 将props.children扁平化后赋值给this.state // 在组件的componentDidAppear生命周期中,state.children包含前后两次props.children的所有子节点 // 通过更新后的props.children判断state.children中的某个节点是否移除,触发该子节点componentWillLeave方法,及更新state _this.state = { children: ReactTransitionChildMapping.getChildMapping(_this.props.children) }, // componentDidMount生命周期内执行,子节点一经挂载,即执行子组件的componentWillAppear、componentDidAppear方法 _this.performAppear = function (key) { _this.currentlyTransitioningKeys[key] = true; var component = _this.refs[key]; if (component.componentWillAppear) { component.componentWillAppear(_this._handleDoneAppearing.bind(_this, key)); } else { // 子节点没有componentWillAppear方法,调用_handleDoneAppearing执行子节点的componentDidAppear方法 // 当子节点componentDidAppear方法内促使ReactTransitionGroup的props.children删除该子节点时,调用performLeave // 何种代码书写方式下发生??? _this._handleDoneAppearing(key); } }, // 作为子节点componentWillAppear方法的回调,触发子节点的componentDidAppear方法 // 当子节点componentWillAppear方法内促使ReactTransitionGroup的state.children不再包含该子节点时,调用performLeave _this._handleDoneAppearing = function (key) { var component = _this.refs[key]; if (component.componentDidAppear) { component.componentDidAppear(); } delete _this.currentlyTransitioningKeys[key]; var currentChildMapping = ReactTransitionChildMapping.getChildMapping(_this.props.children); if (!currentChildMapping || !currentChildMapping.hasOwnProperty(key)) { _this.performLeave(key); } }, // 组件props.children新增子节点引起重绘时,componentDidUpdate生命周期内触发performEnter方法 _this.performEnter = function (key) { _this.currentlyTransitioningKeys[key] = true; var component = _this.refs[key]; if (component.componentWillEnter) { component.componentWillEnter(_this._handleDoneEntering.bind(_this, key)); } else { _this._handleDoneEntering(key); } }, _this._handleDoneEntering = function (key) { var component = _this.refs[key]; if (component.componentDidEnter) { component.componentDidEnter(); } delete _this.currentlyTransitioningKeys[key]; var currentChildMapping = ReactTransitionChildMapping.getChildMapping(_this.props.children); if (!currentChildMapping || !currentChildMapping.hasOwnProperty(key)) { _this.performLeave(key); } }, // 组件props.children删除子节点引起重绘时,componentDidUpdate生命周期内触发performLeave方法 // 最终删除时,调用setState方法移除state.children中的该节点 _this.performLeave = function (key) { _this.currentlyTransitioningKeys[key] = true; var component = _this.refs[key]; if (component.componentWillLeave) { component.componentWillLeave(_this._handleDoneLeaving.bind(_this, key)); } else { _this._handleDoneLeaving(key); } }, _this._handleDoneLeaving = function (key) { var component = _this.refs[key]; if (component.componentDidLeave) { component.componentDidLeave(); } delete _this.currentlyTransitioningKeys[key]; var currentChildMapping = ReactTransitionChildMapping.getChildMapping(_this.props.children); if (currentChildMapping && currentChildMapping.hasOwnProperty(key)) { _this.performEnter(key); } else { _this.setState(function (state) { var newChildren = _assign({}, state.children); delete newChildren[key]; return { children: newChildren }; }); } }, _temp), _possibleConstructorReturn(_this, _ret); } ReactTransitionGroup.prototype.componentWillMount = function componentWillMount() { this.currentlyTransitioningKeys = {};// 以键值对形式记录子节点的componentWillAppear等方法执行过程中信息 this.keysToEnter = [];// 存储组件props变更引起重绘时的新增子节点key值 this.keysToLeave = [];// 存储组件props变更引起重绘时的删除子节点key值 }; ReactTransitionGroup.prototype.componentDidMount = function componentDidMount() { var initialChildMapping = this.state.children; for (var key in initialChildMapping) { if (initialChildMapping[key]) { this.performAppear(key); } } }; // props变更时,state.children存储props.children变更前后的两份扁平化数据 // this.keysToEnter | keysToLeave存储props.children中增或删的子节点key值 ReactTransitionGroup.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var nextChildMapping = ReactTransitionChildMapping.getChildMapping(nextProps.children); var prevChildMapping = this.state.children; // 将props.children变更前后的两份扁平化数据存入this.state.children // 通过componentDidUpdate生命周期内执行的performLeave方法剔除已删除的子节点 this.setState({ children: ReactTransitionChildMapping.mergeChildMappings(prevChildMapping, nextChildMapping) }); var key; // props.children更新前没有key,更新后有key,将key推入this.keysToEnter for (key in nextChildMapping) { var hasPrev = prevChildMapping && prevChildMapping.hasOwnProperty(key); if (nextChildMapping[key] && !hasPrev && !this.currentlyTransitioningKeys[key]) { this.keysToEnter.push(key); } } // props.children更新后没有key,更新前有key,将key推入this.keysToLeave for (key in prevChildMapping) { var hasNext = nextChildMapping && nextChildMapping.hasOwnProperty(key); if (prevChildMapping[key] && !hasNext && !this.currentlyTransitioningKeys[key]) { this.keysToLeave.push(key); } } }; // 依照props.children的增减项执行performEnter以及performLeave方法 ReactTransitionGroup.prototype.componentDidUpdate = function componentDidUpdate() { var keysToEnter = this.keysToEnter; this.keysToEnter = []; keysToEnter.forEach(this.performEnter); var keysToLeave = this.keysToLeave; this.keysToLeave = []; keysToLeave.forEach(this.performLeave); }; ReactTransitionGroup.prototype.render = function render() { // 设置ReactTransitionGroup的refs引用各子节点 var childrenToRender = []; for (var key in this.state.children) { var child = this.state.children[key]; if (child) { childrenToRender.push(React.cloneElement(this.props.childFactory(child), { ref: key, key: key })); } } var props = _assign({}, this.props); delete props.transitionLeave; delete props.transitionName; delete props.transitionAppear; delete props.transitionEnter; delete props.childFactory; delete props.transitionLeaveTimeout; delete props.transitionEnterTimeout; delete props.transitionAppearTimeout; delete props.component; // props.component作为容器节点 // props.children扁平化后添加为ReactTransitionGroup的refs引用,作为props.component容器的子节点 return React.createElement(this.props.component, props, childrenToRender); }; return ReactTransitionGroup; }(React.Component); ReactTransitionGroup.displayName = 'ReactTransitionGroup'; ReactTransitionGroup.propTypes = { component: React.PropTypes.any,// 子节点的容器 childFactory: React.PropTypes.func// 劫持渲染子节点的函数 }; ReactTransitionGroup.defaultProps = { component: 'span', childFactory: emptyFunction.thatReturnsArgument }; // 操控子节点componentWillAppear等方法的执行时机,以及由上层组件提供的childFactory方法劫持渲染子节点 module.exports = ReactTransitionGroup;