如何在reactJs中将字符串数据从后端转换为简单文本因此,像<br/>这样的html标签可以生效

时间:2018-10-20 14:17:48

标签: javascript html reactjs

API后端响应采用JSON形式。一些JSON字段包含html字段,如
。 React正在将此值呈现为字符串,因为
这样的html标签没有生效。如何在reactjs中处理此问题。

from bs4 import BeautifulSoup
html = """
<div class="content" name="content-name">
   <h2 class="Topic">First Topic</h2>
   <ul>
      <li>This Data 1</li>
      <li>This Data 2</li>
      <li>This Data 3</li>
   </ul>
   <h2 class="Topic">Second Topic</h2>
   <ul>
      <li>That Data 1</li>
      <li>That Data 2</li>
      <li>That Data 3</li>
   </ul>
   <h2 class="Topic">Third Topic</h2>
   <ul>
      <li>Their Data 1</li>
      <li>Their Data 2</li>
      <li>Their Data 3</li>
   </ul>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')

for ul in soup.find_all('ul'):
    if ul.find_previous_sibling('h2').text == 'Second Topic':
        for li in ul.find_all('li'):
            print(li.text)

1 个答案:

答案 0 :(得分:0)

您可以使用dangerouslySetInnerHTML道具,如下所示:

  <div dangerouslySetInnerHTML={{__html: 'some <br> HTML <br> string'}}/>

Here's the official documentation for it.