ReactPDF:将html字符串解析为html并显示



我目前正在解析HTML字符串到HTML的问题是,当我试图将其显示为pdf。我正在使用PDFDownloadLink

这是我要解析的条目数组

const unit = [
{
_id: 1,
text: '<p>hello there</p>',
},
{
_id: 2,
text: '<h1>hello there</h1>',
}
{
_id: 3,
text: '<p>hello there</p><br/> <p>Hello World</p>',
}
] 

JSX

(units.map((unit) => {
let text = <p>hello</p>
if (unit.text) {
const parser = new DOMParser()
const doc = parser.parseFromString(unit.text, 'text/html')
// doc.body.firstChild
}
return (
{/* <div dangerouslySetInnerHTML={{ __html: unit.text }} /> */}
<Text>
<div>
{text}
</div>
</Text>
)
})

当我将文本设置为doc.body.firstChild

我得到这个错误信息:

Error: Objects are not valid as a React child (found: [object HTMLParagraphElement]). If you meant to render a collection of children, use an array instead.

对于将来有相同问题的任何人,我找到了一个包(https://www.npmjs.com/package/react-pdf-html),因为它读取它将HTML呈现到reactpdf

最新更新