Reactjs convert html string to jsx

前端 未结 9 1303
夕颜
夕颜 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

    By default, React escapes the HTML to prevent XSS (Cross-site scripting). If you really want to render HTML, you can use the dangerouslySetInnerHTML property:

    <td dangerouslySetInnerHTML={{__html: this.state.actions}} />
    

    React forces this intentionally-cumbersome syntax so that you don't accidentally render text as HTML and introduce XSS bugs.

    0 讨论(0)
  • 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 = "<p><b>Lorem ipsum dolor laboriosam.</b> </p><p>Facere debitis impedit doloremque eveniet eligendi reiciendis <u>ratione obcaecati repellendus</u> culpa? Blanditiis enim cum tenetur non rem, atque, earum quis, reprehenderit accusantium iure quas beatae.</p><p>Lorem ipsum dolor sit amet <a href='#testLink'>this is a link, click me</a> Sunt ducimus corrupti? Eveniet velit numquam deleniti, delectus  <ol><li>reiciendis ratione obcaecati</li><li>repellendus culpa? Blanditiis enim</li><li>cum tenetur non rem, atque, earum quis,</li></ol>reprehenderit accusantium iure quas beatae.</p>"
    
    <Markup content={articleContent} /> // 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
    
    0 讨论(0)
  • 2020-11-22 10:31

    i found this js fiddle. this works like this

    function unescapeHTML(html) {
        var escapeEl = document.createElement('textarea');
        escapeEl.innerHTML = html;
        return escapeEl.textContent;
    }
    
    <textarea className="form-control redactor"
                              rows="5" cols="9"
                              defaultValue={unescapeHTML(this.props.destination.description)}
                              name='description'></textarea>
    

    jsfiddle link

    0 讨论(0)
  • 2020-11-22 10:31

    You can also use Parser() from html-react-parser. I have used the same. Link shared.

    0 讨论(0)
  • 2020-11-22 10:32

    i start using npm package called react-html-parser

    0 讨论(0)
  • 2020-11-22 10:33
    npm i html-react-parser;
    
    import Parser from 'html-react-parser';
    
    <td>{Parser(this.state.archyves)}</td>
    
    0 讨论(0)
提交回复
热议问题