Reactjs convert html string to jsx

前端 未结 9 1309
夕颜
夕颜 2020-11-22 10:09

I\'m having trouble dealing with facebook\'s ReactJS. Whenever I do ajax and want to display an html data, ReactJS displays it as text. (See figure below)

9条回答
  •  情歌与酒
    2020-11-22 10:16

    I recommend using Interweave created by milesj. Its a phenomenal library that makes use of a number if ingenious techniques to parse and safely insert HTML into the DOM.

    Interweave is a react library to safely render HTML, filter attributes, autowrap text with matchers, render emoji characters, and much more.

    • Interweave is a robust React library that can:
      • Safely render HTML without using dangerouslySetInnerHTML.
      • Safely strip HTML tags.
      • Automatic XSS and injection protection.
      • Clean HTML attributes using filters.
      • Interpolate components using matchers.
      • Autolink URLs, IPs, emails, and hashtags.
      • Render Emoji and emoticon characters.
      • And much more!

    Usage Example:

    import React from 'react';
    import { Markup } from 'interweave';
    
    const articleContent = "

    Lorem ipsum dolor laboriosam.

    Facere debitis impedit doloremque eveniet eligendi reiciendis ratione obcaecati repellendus culpa? Blanditiis enim cum tenetur non rem, atque, earum quis, reprehenderit accusantium iure quas beatae.

    Lorem ipsum dolor sit amet this is a link, click me Sunt ducimus corrupti? Eveniet velit numquam deleniti, delectus

    1. reiciendis ratione obcaecati
    2. repellendus culpa? Blanditiis enim
    3. cum tenetur non rem, atque, earum quis,
    reprehenderit accusantium iure quas beatae.

    " // this will take the articleContent string and convert it to HTML markup. See: https://milesj.gitbook.io/interweave //to install package using npm, execute the command npm install interweave

提交回复
热议问题