将React.element转换为JSX字符串

Bha*_*lli 9 javascript reactjs

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

  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]

小智 18

React.renderToString已折旧React v0.14.0,建议ReactDOMServer.renderToString改用。

例如

import ReactDOMServer from 'react-dom/server'
...
ReactDOMServer.renderToString(YourJSXElement())
Run Code Online (Sandbox Code Playgroud)


Hen*_*son 15

如果你想要一个React元素的HTML表示,你可以使用#renderToString#renderToStaticMarkup.

React.renderToString(<div>p</div>);
React.renderToStaticMarkup(<div>p</div>);
Run Code Online (Sandbox Code Playgroud)

会产生

<div data-reactid=".1" data-react-checksum="-1666119235">p</div>
Run Code Online (Sandbox Code Playgroud)

<div>p</div>
Run Code Online (Sandbox Code Playgroud)

分别.但是,您无法将父项本身呈现为字符串.如果您也需要父级,那么从您渲染父级的地方将其renderToString版本作为道具传递给自己.

  • 自React 0.14.0起,不推荐使用`React.renderToString`,而是使用`ReactDomServer.renderToString`. (30认同)
  • `ReactDOMServer` 可以这样导入:`import ReactDOMServer from 'react-dom/server';` (8认同)

小智 7

您可以使用react-element-to-jsx-string

import React from 'react';
import reactElementToJSXString from 'react-element-to-jsx-string';

console.log(reactElementToJSXString(<div a="1" b="2">Hello, world!</div>));
// <div
//   a="1"
//   b="2"
// >
//   Hello, world!
// </div>
Run Code Online (Sandbox Code Playgroud)


Fra*_*tto 5

您还可以使用Pretty-format包,它是 Jest 的一部分,Jest 使用它来显示 JSX 断言中的差异。

import React from "react";
import prettyFormat from "pretty-format";
import renderer from 'react-test-renderer';
const { ReactTestComponent } = prettyFormat.plugins;

function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
   </div>
  );
}

console.log(
    prettyFormat(renderer.create(<App />), {
        plugins: [ReactTestComponent],
        printFunctionName: true,
    })
);
Run Code Online (Sandbox Code Playgroud)

这将输出类似:

<div
  className="App"
>
  <h1>
    Hello CodeSandbox
  </h1>
  <h2>
    Start editing to see some magic happen!
  </h2>
</div>
Run Code Online (Sandbox Code Playgroud)

您可以在这个CodeSandbox中自行尝试:https://codesandbox.io/s/xvxlw370xp