相关疑难解决方法(0)

将React.element转换为JSX字符串

我正在尝试构建一个组件,

  1. 带孩子和
  2. 渲染DOM中的子项,以及
  3. pre出于文档的目的显示子DOM

一种解决方案是将JSX作为单独的一部分传递prop.这使得它重复,因为我已经能够通过它访问它this.props.children.理想情况下,我只需要以某种方式将childrenprop 转换为a,string以便我可以在a中呈现它pre以显示"此代码生成此结果".

这就是我到目前为止所拥有的

class DocumentationSection extends React.Component{
        render(){
           return <div className="section"> 
                    <h1 className="section__title">{heading || ""}</h1> 
                    <div className="section__body"> {this.props.children}</div>
                    <pre className="section__src">
                        //Change this to produce a JSX string from the elements
                        {this.props.children}
                    </pre>
                </div>;
         }  
}
Run Code Online (Sandbox Code Playgroud)

'<Div className='myDiv'>...</Div>当我将DocumentationSection渲染为时,如何以格式获取jsx字符串

<DocumentationSection heading='Heading 1'>
    <Div className='myDiv'>...</Div>
</DocumentationSection>
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:

我试过toString,它dint work,给了[object Object]

javascript reactjs

9
推荐指数
4
解决办法
2万
查看次数

标签 统计

javascript ×1

reactjs ×1