简体   繁体   English

从危险地反应返回标记SetInnerHTML

[英]react return markup from dangerouslySetInnerHTML

I have html saved from wysiwyg editor and fetched from database, I need to display the content which I usually do with 我从wysiwyg编辑器中保存了html,并从数据库中获取了html,我需要显示我通常使用的内容

 <div dangerouslySetInnerHTML={{__html: content}} />

but now I need to use the formatted content inside another component, 但现在我需要在另一个组件中使用格式化的内容,

so I am hoping if there is a function to return the markup like this var formattedContent = getMarkup(content)//expecting this to do what dangerouslySetInnerHTML does 所以我希望是否有一个函数可以像这样返回标记var formattedContent = getMarkup(content)//期望这样做可以完成危险的SetInnerHTML

<Highlight search="Condominium" >
    {formattedContent}
</Highlight>

because this isn't working 因为这不起作用

<Highlight search="Condominium" >
      <div dangerouslySetInnerHTML={{__html: content}} />
</Highlight>

Any help is appreciated as I am nearly struggling for with this more than a day. 感谢任何帮助,因为我几乎每天都在为此苦苦挣扎。

What I am trying above is 我在上面尝试的是

npmjs.com/package/react-highlighter -- it highlight matched words, but I need to highlight not just plain text but html npmjs.com/package/react-highlighter-突出显示匹配的单词,但我不仅要突出显示纯文本,还要突出显示html

The dangerouslySetInnerHTML value is an object with __html as key and your HTML content as value. dangerouslySetInnerHTML值是一个以__html为键,而您的HTML内容为值的对象。

<Highlight search="Condominium" >
    <div dangerouslySetInnerHTML={{__html: content}} />
</Highlight>

Update: 更新:

The Highlight component has already an Attribute innerHTML . Highlight组件已经具有一个属性innerHTML You don't need to use dangerouslySetInnerHTML in your case. 在这种情况下,您无需dangerouslySetInnerHTML地使用dangerouslySetInnerHTML

From official Documentation : 从官方文件

<Highlight innerHTML={true}>{content}</Highlight>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM