欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

深入理解React16源码:HostComponent与HostText的内部工作机制

最编程 2024-01-21 17:11:58
...
function updateHostComponent(current, workInProgress, renderExpirationTime) { // 这个先跳过 pushHostContext(workInProgress); if (current === null) { // 对于整个应用来讲,如果需要复用服务端渲染返回的dom内容,只有 HostComponent 和 HostText // 是需要被复用的,对于 class component 和 function component 本身是不对应于dom的某个节点的 // 不会调用 hydrate 相关的东西 tryToClaimNextHydratableInstance(workInProgress); } // 获取type和props, 对于 HostComponent 没有 state的概念 const type = workInProgress.type; const nextProps = workInProgress.pendingProps; const prevProps = current !== null ? current.memoizedProps : null; let nextChildren = nextProps.children; const isDirectTextChild = shouldSetTextContent(type, nextProps); // 获取该节点是否是 纯字符串 if (isDirectTextChild) { // We special case a direct text child of a host node. This is a common // case. We won't handle it as a reified child. We will instead handle // this in the host environment that also have access to this prop. That // avoids allocating another HostText fiber and traversing it. nextChildren = null; } else if (prevProps !== null && shouldSetTextContent(type, prevProps)) { // If we're switching from a direct text child to a normal child, or to // empty, we need to schedule the text content to be reset. // 之前props存在,并且也是文本,则重新设置 tag workInProgress.effectTag |= ContentReset; } // 在 class component 和 host component 都有这个 // 在 class component 是有 instance // 在 dom 节点,也有 instance // 对应这两种节点才能拿到 ref, 否则没有引用 markRef(current, workInProgress); // Check the host config to see if the children are offscreen/hidden. // 符合 下面这种情况,其中 当前的更新的优先级不为 Never, workInProgress.mode 要符合 ConcurrentMode 并且 设置了 hidden // 比如模拟自定义的滑动,浏览器的滚动条,通过这个属性来进行一个优化,把不需要显示的组件设置为 hidden // 这样每次滑动,就不需要更新这个组件,减少损耗性能 if ( renderExpirationTime !== Never && workInProgress.mode & ConcurrentMode && shouldDeprioritizeSubtree(type, nextProps) ) { // Schedule this fiber to re-render at offscreen priority. Then bailout. workInProgress.expirationTime = Never; // 设置成 Never 这个节点将永不会更新到 return null; } // 调用来创建,调和子节点 reconcileChildren( current, workInProgress, nextChildren, renderExpirationTime, ); return workInProgress.child; }