React的渲染"0"问题及源码分析

简介: React的渲染"0"问题及源码分析

开门见山,先来看一张bug图(状态下面有个00)。


image.png


预期是:状态为0时,2个组件不做渲染。

现状:状态为0时,2个组件不做渲染,但是渲染出了00。

  • 零渲染 bug 代码
  • 如何修复零渲染问题
  • 初窥源码
  • 源码疑惑
  • 原因总结
  • 源码实锤


零渲染 bug 代码



什么是React的零渲染问题?


看下下面这段代码,我们会经常这样写:


// bug代码 0
{obj?.count && <span>{obj?.count}</span>}


假如obj?.count为0,渲染结果为0。

这里不就1个0么,上面为什么是00呢。

// bug代码 00 
{obj?.countFoo && <span>{obj?.countFoo}</span>}
{obj?.countBar && <span>{obj?.countBar}</span>}


当obj?.countFoo和obj?.countBar都为0时,渲染结果为00。


如何修复零渲染问题

{!!obj?.count && <span>{obj?.count}</span>}

或者

{obj?.count ? <span>{obj?.count}</span> : null}

或者

{Boolean(obj?.count) && <span>{obj?.count}</span>}


初窥源码


原因(点击类型查看源码):

React组件会渲染string,number。不会渲染nullundefinedboolean


源码疑惑


既然boolean会被处理为null,那为什么true && <FooComponent />可以正常渲染呢?


先说结论,因为进行了&&运算,React最终渲染的是jsx与计算后的结果。


const type = typeof children;
if (type === 'undefined' || type === 'boolean') {
    // All of the above are perceived as null.
    children = null;
}


也就是说 此处的children,是jsx计算后的结果。


举例如下:

// 可渲染值
1 && <FooComponent /> // => jsx计算结果为<FooComponent />,因此渲染<FooComponent/>
"a string" && <FooComponent /> // => jsx计算结果为<FooComponent />,因此渲染<FooComponent />
0 && <FooComponent /> // => jsx计算结果为0,Renders '0'
true && <FooComponent /> // => jsx计算结果为<FooComponent />,因此渲染<FooComponent />
// 不可渲染值
false && <FooComponent /> // => jsx计算结果为false,因此什么都不渲染
null && <FooComponent /> // => jsx计算结果为null,因此什么都不渲染
undefined && <FooComponent /> // => jsx计算结果为undefined,因此什么都不渲染


原因总结


其实,根本不是React渲染什么的问题,而是&&操作符后返回值的问题。

所以,最根本是因为

  • React渲染string,number,正常组件
  • React不渲染undefined,boolean,null


{"1"} // 渲染为"1"
{0} // 渲染为0
{<FooComponent />} // 假设为正常组件,渲染为<FooComponent />
{undefined} // 不渲染
{true} // 不渲染
{false} // 不渲染
{null} // 不渲染


源码实锤

  const type = typeof children;
  // React不渲染undefined,boolean
  if (type === 'undefined' || type === 'boolean') {
    // All of the above are perceived as null.
    children = null;
  }
  let invokeCallback = false;
  if (children === null) {
    invokeCallback = true;
  } else {
    switch (type) {
      case 'string':
      case 'number':
        // React渲染string,number
        invokeCallback = true; 
        break;
      case 'object':
        // React渲染正常组件
        switch ((children: any).$$typeof) {
          case REACT_ELEMENT_TYPE:
          case REACT_PORTAL_TYPE:
            invokeCallback = true;
        }
    }
  }


原始值为null,和undefined以及boolean最终被处理为null,React不渲染null的源码实锤呢


  render(
    child: ReactNode | null,
    context: Object,
    parentNamespace: string,
  ): string {
    if (typeof child === 'string' || typeof child === 'number') {
      const text = '' + child;
      if (text === '') {
        return '';
      }
      this.previousWasTextNode = true;
      return escapeTextForBrowser(text);
    } else {
      let nextChild;
      ({child: nextChild, context} = resolve(child, context, this.threadID));
      // React不渲染null
      if (nextChild === null || nextChild === false) {
        return '';
      }


对于html标签渲染空字符串而言,空字符串会被渲染,例如<div>""</div>会被渲染为<div>""</div>

但对于react而言,完整流程为{null} =>{""} => nothing

例如下面这样:

<div>{''}</div> // => <div></div>
<div>{'    '}</div> // => <div>    </div>

那么,React是如何处理空字符串的呢?

export function pushTextInstance(
  target: Array<Chunk | PrecomputedChunk>,
  text: string,
  responseState: ResponseState,
): void {
  if (text === '') {
    // Empty text doesn't have a DOM node representation and the hydration is aware of this.
    // 这句注释的意思是:空文本节点没有DOM节点表示,它属于textNode
    return;
  }
  // TODO: Avoid adding a text separator in common cases.
  target.push(stringToChunk(encodeHTMLTextNode(text)), textSeparator);
}

从源码我们可以看到,对于空文本节点,React会直接return出去,不会去生成文本实例(TextInstance)。





相关文章
|
1天前
|
前端开发 JavaScript
React如何进行条件渲染
React如何进行条件渲染
12 0
|
1天前
|
存储 前端开发 JavaScript
在回调函数中重新渲染React组件
在React中,重新渲染组件可通过`forceUpdate()`或`ReactDOM.render()`实现。方法一是使用`forceUpdate`强制无状态组件更新;方法二是通过重新创建根组件实例适用于有状态组件。这些示例基于Webpack和Babel的模块热替换配置。根据项目需求和React版本,还可以结合React-Router或Redux等库选择合适的方法。
|
1天前
|
存储 JavaScript 算法
React聚焦渲染速度
React聚焦渲染速度
23 0
|
1天前
|
存储 JSON 资源调度
next.js博客搭建_react-markdown渲染内容(第三步)
next.js博客搭建_react-markdown渲染内容(第三步)
9 1
|
1天前
|
数据采集 资源调度 前端开发
React的服务器端渲染:使用ReactDOMServer进行高效页面预渲染
【4月更文挑战第25天】使用ReactDOMServer,React支持服务器端渲染以实现高效预渲染。通过在Node.js环境中将React组件转化为HTML字符串,减少客户端JavaScript负载和渲染时间。优点包括更快首屏加载、改善SEO和兼容无JavaScript环境,但也会增加服务器负载、复杂性和状态管理挑战。开发者需根据项目需求平衡SSR和CSR。
|
1天前
|
前端开发 JavaScript
React渲染性能的优化
React渲染性能的优化
27 2
|
1天前
|
前端开发 JavaScript 安全
react如何渲染包含html标签元素的字符串
react如何渲染包含html标签元素的字符串
65 0
|
1天前
|
前端开发
React 中条件渲染的 N 种方法
React 中条件渲染的 N 种方法
21 3
|
1天前
|
前端开发 JavaScript
react的render什么时候渲染?
react的render什么时候渲染?
20 0
|
1天前
|
前端开发 JavaScript
React中通过children prop或者React.memo来优化子组件渲染【react性能优化】
React中通过children prop或者React.memo来优化子组件渲染【react性能优化】
46 0
http://www.vxiaotou.com