实现说明
实现说明
这一部分是关于 stack reconciler 的一些实现说明。
这部分比较具有技术性,需要对 React 公共 API,以及 React 是如何将其分为 core、renderer 和 reconciler 的具有较好的理解。如果你对源码库还不是很熟悉,请先阅读源码总览。
这部分还要求了解 React 组件及其实例和元素之间的不同。
stack reconciler 是在 React 15 以及更早的版本中被采用。它的源码位于 src/renderers/shared/stack/reconciler。
视频:从零开始构建 React
Paul O’Shannessy 讲解的从零开始构建 React 对本文档有较大的启发。
本文档和他的讲解都是对实际代码库的简化,所以你能通过熟悉它们来获得更好的理解。
概览
reconciler 本身没有公共的 API。像 React DOM 和 React Native 这样的 renderer 使用它来根据用户写的 React 组件来高效地更新用户界面。
挂载是递归过程
让我们考虑第一次挂载组件时:
ReactDOM.render(<App />, rootEl);
React DOM 把 <App /> 传递给 reconciler。请记住,<App /> 是一个 React 元素,也就是对要渲染的内容的描述。可以把它视为普通的对象:
console.log(<App />);// { type: App, props: {} }
reconciler 检查 App 是一个类还是一个函数。
如果 App 是函数,那么 reconciler 会调用 App(props) 来获取渲染的元素。
如果 App 是类,那么 reconciler 会通过 new App(props) 来实例化 App,并调用生命周期方法 componentWillMount(),之后调用 render() 方法来获取渲染的元素。
无论哪种方式,reconciler 都会探悉 App 的内容并渲染。
这个过程是递归的。App 可能会渲染某个 <Greeting />,Greeting 可能会渲染某个 <Button />,以此类推。当它探悉各个组件渲染的元素时,reconciler 会通过用户定义的组件递归地 “向下探索”。
通过以下伪代码想象一下这个过程:
function isClass(type) { // 类组件会有这个标识位 return ( Boolean(type.prototype) && Boolean(type.prototype.isReactComponent) );}// 这个函数接受一个 React 元素 (例如: <App />)// 并返回表示已挂载树的 DOM 或者 原生节点function mount(element) { var type = element.type; var props = element.props; // 将通过 type 作为函数运行 // 或创建实例并调用 render() // 返回渲染后的元素 var renderedElement; if (isClass(type)) { // 类组件 var publicInstance = new type(props); // 设置 props publicInstance.props = props; // 如果有生命周期方法就调用 if (publicInstance.componentWillMount) { publicInstance.componentWillMount(); } // 调用 render() 返回渲染后的元素 renderedElement = publicInstance.render(); } else { // 函数组件 renderedElement = type(props); } // 这个过程是递归的 // 因为组件可能会返回具体另一个组件类型的元素 return mount(renderedElement); // 提示:这个实现是不完整的并且无限递归! // 只处理像 <App /> 或者 <Button /> 的元素 // 还不能处理像 <div /> 或者 <p /> 的元素}var rootEl = document.getElementById('root');var node = mount(<App />);rootEl.appendChild(node);
注意:
这其实是一份伪代码。它与真实的实现并不相似。因为我们还没有讨论该递归过程何时停止,所以它也会造成堆栈溢出。
让我们回顾上面例子中的一些关键的想法:
-
React 元素是用来表示组件的类型(例如:App)和 props 的简单的对象。
-
用户定义的组件(例如:App)可以是类,也可以是函数,但是它们都“渲染产生”元素。
-
“挂载”是一个递归的过程,根据特定的顶层 React 元素(e.g. <App />)产生 DOM 或 Native 树。
挂载宿主元素
如果我们没有渲染某些东西输出到电脑屏幕,这个过程将会是无用的。
除了用户定义的(“组合”)组件,React 元素也可能表示为平台专属(“宿主”)组件。例如,Button 可能会从 render 方法返回一个 <div />。
如果元素的 type 属性是字符串,我们处理的就是宿主元素:
console.log(<div />);// { type: 'div', props: {} }
宿主元素中没有用户定义代码。
当 reconciler 遇到宿主元素时,它会让 renderer 负责挂载它。例如,React DOM 会创建一个 DOM 节点。
如果宿主元素拥有子元素,reconciler 会根据上文提到的算法对其进行递归地挂载。无论子元素是宿主(像 <div><hr /><div>),还是组合(像 <div><Button /></div>),两者都无所谓。
子组件生成的 DOM 节点会附加在父 DOM 节点上,递归地完成整个 DOM 结构的组装。
注意:
reconciler 本身不与 DOM 绑定。挂载的确切结果(在源代码中有时叫做 “挂载映像”)取决于 renderer,可以是一个 DOM 节点(React DOM),一个字符串(React DOM Server),或是一个表示原生视图的数字(React Native)。
如果我们扩展代码去处理宿主元素,会是如下样子:
function isClass(type) { // 类组件会有这个标识位 return ( Boolean(type.prototype) && Boolean(type.prototype.isReactComponent) );}// 此函数仅处理组合类型的元素// 例如,处理 <App /> 和 <Button />, 但不处理 <div />function mountComposite(element) { var type = element.type; var props = element.props; var renderedElement; if (isClass(type)) { // 类组件 var publicInstance = new type(props); // 设置 props publicInstance.props = props; // 如果有生命周期方法就调用 if (publicInstance.componentWillMount) { publicInstance.componentWillMount(); } renderedElement = publicInstance.render(); } else if (typeof type === 'function') { // 函数组件 renderedElement = type(props); } // 这是递归的,但是当元素是宿主(例如: <div />)而不是组合(例如 <App />)时, // 我们最终会到达递归的底部: return mount(renderedElement);}// 此函数只处理宿主类型的元素// 例如: 处理 <div /> 和 <p />,但不处理 <App />.function mountHost(element) { var type = element.type; var props = element.props; var children = props.children || []; if (!Array.isArray(children)) { children = [children]; } children = children.filter(Boolean); // 这段代码不应该出现在 reconciler。 // 不同的 renderer 可能会以不同方式初始化节点。 // 例如,React Native 会创建 iOS 或 Android 的视图。 var node = document.createElement(type); Object.keys(props).forEach(propName => { if (propName !== 'children') { node.setAttribute(propName, props[propName]); } }); // 挂载子元素 children.forEach(childElement => { // 子元素可能是宿主(例如:<div />)或者组合 (例如:<Button />). // 我们还是递归挂载他们 var childNode = mount(childElement); // 这一行代码也是特殊的 renderer。 // 根据 renderer 不同,方式也不同: node.appendChild(childNode); }); // DOM 节点作为挂载的结果返回。 // 这是递归结束的位置。 return node;}function mount(element) { var type = element.type; if (typeof type === 'function') { // 用户定义组件 return mountComposite(element); } else if (typeof type === 'string') { // 平台特定组件 return mountHost(element); }}var rootEl = document.getElementById('root');var node = mount(<App />);rootEl.appendChild(node);
以上代码是可以运作的,但是与 reconciler 的实际实现依然相差很远。关键的缺失部分是对更新的支持。
引入内部实例
React 的关键特点是你可以重新渲染所有内容,并且不会重新生成 DOM 或重置 state:
ReactDOM.render(<App />, rootEl);// 应该重用已经存在的 DOM:ReactDOM.render(<App />, rootEl);
然而,之前的实现只是知道如何挂载最初的树。由于它没有储存所有的必要信息,例如所有的 publicInstance,或 DOM 节点属于哪个组件,所以它不能完成更新操作。
stack reconciler 源码通过把 mount() 函数作为一个类的方法来解决这个问题。这种方法是存在缺点的,所以我们正朝着与之相对的方向进行 reconciler 的重写工作。不过这就是它现在的运作方式。
我们会创建两个类:DOMComponent 和 CompositeComponent,而不是分离的两个函数 mountHost 和 mountComposite。
两个类都有一个接受 element 的构造函数,同时也有一个返回挂载后节点的 mount() 方法。我们用一个可以实例化正确类的工厂函数替换了顶层的 mount() 函数:
function instantiateComponent(element) { var type = element.type; if (typeof type === 'function') { // 用户定义组件 return new CompositeComponent(element); } else if (typeof type === 'string') { // 平台特定组件 return new DOMComponent(element); } }
首先,让我们思考一下 CompositeComponent 的实现:
class CompositeComponent { constructor(element) { this.currentElement = element; this.renderedComponent = null; this.publicInstance = null; } getPublicInstance() { // 对于组合组件,公共类实例 return this.publicInstance; } mount() { var element = this.currentElement; var type = element.type; var props = element.props; var publicInstance; var renderedElement; if (isClass(type)) { // 组件类 publicInstance = new type(props); // 设置 props publicInstance.props = props; // 如果有生命周期方法就调用 if (publicInstance.componentWillMount) { publicInstance.componentWillMount(); } renderedElement = publicInstance.render(); } else if (typeof type === 'function') { // 函数组件 publicInstance = null; renderedElement = type(props); } // 保存公共实例 this.publicInstance = publicInstance; // 根据元素实例化子内部实例。 // <div /> 或者 <p /> 是 DOMComponent, // 而 <App /> 或者 <Button /> 是 CompositeComponent。 var renderedComponent = instantiateComponent(renderedElement); this.renderedComponent = renderedComponent; // 挂载渲染后的输出 return renderedComponent.mount(); }}
这与之前的 mountComposite() 的实现没有太多的不同,但是现在我们可以保存一些信息,如 this.currentElement,this.renderedComponent 和 this.publicInstance,用于更新期间使用。
需要注意的是 CompositeComponent 的实例与用户提供的 element.type 的实例是不同的东西。CompositeComponent 是我们的 reconciler 的实现细节,并且永远不会暴露给用户。用户定义的类是从 element.type 读取的,并且 CompositeComponent 会创建一个它的实例。
为了避免混淆,我们把 CompositeComponent 和 DOMComponent 的实例叫做“内部实例”。由于它们的存在,我们可以把一些长时间存在的数据存入其中。只有 renderer 和 reconciler 能意识到它们的存在。
相反,我们把用户定义的类的实例叫做“公共实例”。公共实例就是你在 render() 中所见到的 this 和你的自定义组件中的一些其他方法。
mountHost() 函数,重构为 DOMComponent 类的 mount() 方法,看起来也很熟悉:
