span是作为文本呈现而不是反应中的元素?

时间:2017-09-05 08:33:38

标签: javascript html reactjs

我正在从api进行查询,api会返回如下结果片段:

"<span class="searchmatch">Justin</span> Drew <span class="searchmatch">Bieber</span> (/ˈbiːbər/; born March 1, 1994) is a Canadian singer and songwriter. After a talent manager discovered him through his YouTube videos"

当我在我的组件中渲染它时:

{this.props.snippet} 

它返回一个文本,但我想将其渲染为html元素。帮助

1 个答案:

答案 0 :(得分:-2)

您很可能正在寻找dangerouslySetInnerHTML

并像这样呈现:

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

请注意,这可能非常危险,如果可能应该避免。