2017-01-11 58 views
0

對於React我很新,我正在創建一個使用react-bootstrap的表單字段的簡單工具集。我正在使用一個<Form>組件來包裝所有內容。該組件將渲染所有的孩子,通過一個新的屬性。根據零件類型有條件地將道具傳遞給兒童

React.Children.map(this.props.children, (child) => { 
    return React.cloneElement(child, { 
     myNewProp: 'hello' 
    } 
}); 

我的問題是,它們不具有的myNewProp結果的道具在警告Unknown props myNewProp,本地的HTML和反應,引導部件孩子的元素。是否有任何方法根據組件的類型有條件地向子元素添加道具?例如。

if (child instanceof <nativeHTMLelement>) { ... } else { ... } 
+0

的可能的複製[比較兩個組成部分 - 是分量x分量A的一個實例(http://stackoverflow.com/questions/27824548 /比較 - 兩個組件是組件-x-組件實例-a) –

回答

0

你可以在這種情況下使用child.type

if (typeof child.type === 'string') { 
    // this is a html element 
} else { 
    // this is a React component 
} 
+0

謝謝!我還添加了一個'child.type.prototype instanceof MyElement'的檢查 – dtsn

相關問題