#### 源码实锤
```js
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;
}
}
}
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);
}
开门见山,先来看一张bug图(状态下面有个00)。
预期是:状态为0时,2个组件不做渲染。
现状:状态为0时,2个组件不做渲染,但是渲染出了00。
零渲染 bug 代码
什么是React的零渲染问题? 看下下面这段代码,我们会经常这样写:
假如obj?.count为0,渲染结果为0。 这里不就1个0么,上面为什么是00呢。
当obj?.countFoo和obj?.countBar都为0时,渲染结果为00。
如何修复零渲染问题
或者
或者
初窥源码
原因(点击类型查看源码): React组件会渲染string,number。不会渲染null,undefined,boolean。
源码疑惑
既然boolean会被处理为null,那为什么
true && <FooComponent />
可以正常渲染呢? 先说结论,因为进行了&&运算,React最终渲染的是jsx与计算后的结果。也就是说 此处的children,是jsx计算后的结果。
举例如下:
原因总结
其实,根本不是React渲染什么的问题,而是&&操作符后返回值的问题。 所以,最根本是因为
{undefined} // 不渲染 {true} // 不渲染 {false} // 不渲染
{null} // 不渲染
原始值为null,和undefined以及boolean最终被处理为null,React不渲染null的源码实锤呢?
对于html标签渲染空字符串而言,空字符串会被渲染,例如
<div>""</div>
会被渲染为<div>""</div>
但对于react而言,完整流程为{null} =>{""} => nothing
例如下面这样:那么,React是如何处理空字符串的呢?
从源码我们可以看到,对于空文本节点,React会直接return出去,不会去生成文本实例(TextInstance)。