2016-08-12 70 views

回答

8

這實際上是React's internal support for things like null部分:

// A perfectly valid component 
() => null 

// Also a perfectly valid component 
const MyComponent = React.createClass({ 
    render() { 
    if (this.props.hidden) return null; 
    return <p>My component implementation</p>; 
    } 
}); 
+0

哇!謝謝!如果我使用ajax來獲取像'

'這樣的服務器渲染組件字符串,我想將它安裝在此節點中。怎麼做。 – Leen

0

看的React這部分代碼是創建這樣的:

var nodeValue = ' react-empty: ' + this._domID + ' '; 
    if (transaction.useCreateElement) { 
     var ownerDocument = hostContainerInfo._ownerDocument; 
     var node = ownerDocument.createComment(nodeValue); 
     ReactDOMComponentTree.precacheNode(this, node); 
     return DOMLazyTree(node); 
    } else { 
     if (transaction.renderToStaticMarkup) { 
     // Normally we'd insert a comment node, but since this is a situation 
     // where React won't take over (static pages), we can simply return 
     // nothing. 
     return ''; 
     } 
     return '<!--' + nodeValue + '-->'; 
    } 
    }, 

所以基本上,如果你的分量返回null,它將創建註釋其中顯示這個元素是空的,但React負責爲你購買那裏的評論<!-- react-empty: 3 -->所有的JavaScript框架試圖在DOM中使用註釋,以顯示它們處理代碼,類似的是ng - 如果在角度f或示例...

0

注意與之反應> = 16,你不會看到<!-- react-empty: X -->