我正在嘗試構建一個組件,
>帶孩子和
>渲染DOM中的子項,以及
>出于文檔的目的,在pre中顯示子DOM
一種解決方案是將JSX作為單獨的prop傳遞.這使得它重復,因為我已經能夠通過this.props.children訪問它.理想情況下,我只需要以某種方式將子prop轉換為字符串,以便我可以在pre中呈現它以顯示“此代碼生成此結果”.
這就是我到目前為止所擁有的
class DocumentationSection extends React.Component{
render(){
return
{heading || ""}
{this.props.children}
//Change this to produce a JSX string from the elements
{this.props.children}
}
}
如何以’< Div className ='myDiv'> …< / Div>格式獲取jsx字符串?當我將DocumentationSection渲染為
...
謝謝.
編輯:
我試過toString,它dint work,給了[object Object]