[英]Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
I'm trying to render LaTeX strings in a React project.我正在尝试在 React 项目中呈现 LaTeX 字符串。 Although I use the react-mathjax
React components, I want to get an HTML string made from the LaTeX strings in order to concatenate it and the other strings and set it by dangerouslySetInnerHTML.虽然我使用react-mathjax
React 组件,但我想获取一个由 LaTeX 字符串组成的 HTML 字符串,以便将它和其他字符串连接起来,并通过危险react-mathjax
设置它。
Sample code is here: https://github.com/Nyoho/test-react-project/blob/component2string/src/components/tex.jsx示例代码在这里: https : //github.com/Nyoho/test-react-project/blob/component2string/src/components/tex.jsx
aDom
by document.createElement('span')
(in background. not in the document DOM tree.)通过document.createElement('span')
aDom
一个空的 DOM aDom
(在后台。不在文档 DOM 树中。)ReactDOM.render
into aDom
通过ReactDOM.render
将 LaTeX 字符串渲染为aDom
aDom.innerHTML
or .outerHTML
渲染后,通过aDom.innerHTML
或.outerHTML
获取字符串The value of aDom.innerHTML
(or .outerHTML
) is "<span><span data-reactroot=\\"\\"></span></span>"
(almost empty) although aDom
has a perfect tree that MathJax generated. aDom.innerHTML
(或.outerHTML
)的值是"<span><span data-reactroot=\\"\\"></span></span>"
(几乎是空的),尽管aDom
有一个由 MathJax 生成的完美树。
Briefly,简要地,
aDom
: 🙆 aDom
: 🙆aDom.outerHTML
: 🙅 aDom.outerHTML
: 🙅 A screenshot console.log of aDom
and aDom.outerHTML
aDom
和aDom.outerHTML
屏幕截图 console.log
How can I get the 'correct' HTML string from aDom
above?如何从上面的aDom
获取“正确”的 HTML 字符串?
This seems to work just fine if you want to render any component to a HTML string: 如果要将任何组件呈现为HTML字符串,这似乎工作得很好:
import { renderToString } from 'react-dom/server'
renderToString() {
return renderToString(<MyAwesomeComponent some="props" or="whatever" />)
}
From what I see, you are getting what you'd expect to get. 从我看到的,你得到了你期望得到的东西。
Given a root element ( aDom
in your case), ReactDOM will render it's root component inside this element, and this component's element will have the attribute data-reactroot
. 给定根元素( aDom
在你的情况下),ReactDOM会使它的根组件在这个元素里,而这部分的元素将有属性data-reactroot
。
So what you are seeing is exactly how it should be. 所以你所看到的正是应该如何。 From what I've tested, the inner dom tree should be there as well. 根据我的测试,内部dom树也应该在那里。
var Another = React.createClass({ render: function() { return ( <div>Just to see if other components are rendered as well</div> ); } }); var Hello = React.createClass({ render: function() { return ( <div id="first"> <div id="sec-1">Hello</div> <div id="sec-2">{ this.props.name }</div> <Another /> </div> ); } }); var a = document.createElement('div'); ReactDOM.render( <Hello name = "World" /> , a ); console.log(a.outerHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
The result in the console is: 控制台中的结果是:
<div><div data-reactroot="" id="first"><div id="sec-1">Hello</div><div id="sec-2">World</div><div>Just to see if other components are rendered as well</div></div></div>
I recommend using renderToStaticMarkup
over renderToString
as it does not add any extra DOM attributes that React uses internally, like `data-reactroot:我建议使用renderToStaticMarkup
不是renderToString
因为它不会添加任何 React 内部使用的额外 DOM 属性,例如 `data-reactroot:
import { renderToStaticMarkup } from 'react-dom/server'
getString() {
return renderToStaticMarkup(<AnyComponent />)
}
Documentation:文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.