最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to render HTML emails in React app - Stack Overflow

programmeradmin2浏览0评论

I create an SPA React application that can send and receive email messages.

What is the best way to render received HTML email messages? The task bees problematic when I receive a huge email message with a lot of images in base64 and HTML tags.

I receive the email as JSON HTML string from API and want to render it properly.

I tried two approaches:

  1. Render in div with dangerouslySetInnerHTML={{ __html: htmlMessageText }} - there is problem with safety
  2. Use my email editor in preview mode (Jodit) - there is problem with Layout Thrashing

In both approaches, there is the problem with performance. Emails (800 lines) renders a lot of time (~2 minutes).

There is no problem with simple HTML emails, these renders fast, but with some of them - especially bigger emails - application like to freeze layout.

I create an SPA React application that can send and receive email messages.

What is the best way to render received HTML email messages? The task bees problematic when I receive a huge email message with a lot of images in base64 and HTML tags.

I receive the email as JSON HTML string from API and want to render it properly.

I tried two approaches:

  1. Render in div with dangerouslySetInnerHTML={{ __html: htmlMessageText }} - there is problem with safety
  2. Use my email editor in preview mode (Jodit) - there is problem with Layout Thrashing

In both approaches, there is the problem with performance. Emails (800 lines) renders a lot of time (~2 minutes).

There is no problem with simple HTML emails, these renders fast, but with some of them - especially bigger emails - application like to freeze layout.

Share Improve this question edited Sep 12, 2018 at 9:05 Sumesh TG 2,5752 gold badges16 silver badges29 bronze badges asked Sep 12, 2018 at 8:00 crowmwcrowmw 2481 gold badge3 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

The performance problems are caused by big data-URI images. The safety problems can be solved by a sanitizer library. So your algorithm might look like this:

  1. Replace data: URIs with blob:. For inspiration you can check out the relevant part in the code of TinyMCE.
  2. Now that after step 1 there is much less markup left, pass it through an HTML sanitizer. I would remend DOMPurify.
  3. dangerouslySetInnerHTML

We found a library that addresses this issue. It's called react-letter.

We were also facing the same issue. We initially used to purify the html and convert html to uri and pass it iframe. It worked but if the uri is more than 2mb it used to break. The react-letter library is working for now.

You could try a HTML to React parser... https://www.npmjs./package/react-html-parser.

You can use the ref elements to pass HTML in your react ponent

class Demo extends Component {
    constructor(props) {
        super(props);

    }

    render() {
         //Here you can add your Html code 
         //or you can pass by props or any state
         this.el.innerHTML = '';

        return (
            <div ref={el => this.el = el}>
            </div>
        );
    }

}

export default Demo
发布评论

评论列表(0)

  1. 暂无评论