简体   繁体   English

使用 react html-react-parser 或 innerHTML 解析 html 字符串? 哪个更快?

[英]parse html string using react html-react-parser or innerHTML? which is faster?

I receive html string from backend.我从后端收到 html 字符串。 I'm parsing html string using react-html-parser.我正在使用 react-html-parser 解析 html 字符串。 But the package's import size is 174kb .但是包的导入大小是174kb I can do the same thing using innerHTML .我可以使用innerHTML做同样的事情。 Please refer to code below.请参考下面的代码。 I'm setting innerHTML in useEffect so that changes are reflected when text changes.我在 useEffect 中设置 innerHTML,以便在text更改时反映更改。

  • Using innerHTML使用 innerHTML
  useEffect(() => {
    document.getElementById(`text-${_id}`).innerHTML = text;
}, [text]);

  • using parser (I can do it in return statement itself)使用解析器(我可以在 return 语句本身中完成)
function SomeComponent({_id}) {
return (
    <div className="list-group-item mb-3">
      <div id={`text-${_id}`}>
        {parser(text)}
      </div>
    </div>
)}

I looked at the import size using importCost extension.我使用importCost扩展查看了导入大小。 Acc.累积to you, Which method will be faster.对你来说,哪种方法会更快。 Is it worth using html-react-parser ?是否值得使用html-react-parser

在此处输入图像描述

React is a JavaScript library and it internally uses JavaScript so, it will be slower. React 是一个 JavaScript 库,它内部使用 JavaScript 所以,它会更慢。

If it's just about setting an html value, you can use .innerHTML , but there are security considerations for its usage.如果只是设置一个 html 值,你可以使用.innerHTML ,但它的使用有安全方面的考虑

To avoid injection attacks(like XSS), we should sanitize the html while insertion(even if the DB is storing sanitized html).为了避免注入攻击(如 XSS),我们应该在插入时清理 html(即使数据库正在存储清理过的 html)。

Use a sanitizer like sanitize-html or react-sanitize-html like this使用像sanitize-htmlreact-sanitize-html这样的消毒剂

import SanitizedHTML from 'react-sanitized-html';

function SomeComponent({_id}) {
return (
    <div className="list-group-item mb-3">
      <div id={`text-${_id}`}
        <SanitizedHTML html={ text} />
      </div>
    </div>
)}

html-to-react parser (which internally calls html-to-dom-parser ) is react specific and serves a different purpose. html-to-react解析器(内部调用html-to-dom-parser )是特定于react的,并且服务于不同的目的。 If you look at it's signature,如果你看它的签名,

/**
 * Converts HTML string to React elements.
 *
 * @param {string} html - HTML string.
 * @param {object} [options] - Parser options.
 * @param {object} [options.htmlparser2] - htmlparser2 options.
 * @param {object} [options.library] - Library for React, Preact, etc.
 * @param {Function} [options.replace] - Replace method.
 * @returns {JSX.Element|JSX.Element[]|string} - React element(s), empty array, or string.
 */
function HTMLReactParser(html, options)

you can see that it returns React element(s), empty array, or string你可以看到它返回React 元素、空数组或字符串


Please note that Element.innerHTML is not DOM parsing.请注意Element.innerHTML不是 DOM 解析。 Its different.这不一样。 AS per MDN Docs,根据 MDN 文档,

The Element property innerHTML gets or sets the HTML or XML markup contained within the element. Element 属性 innerHTML 获取或设置元素中包含的 HTML 或 XML 标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM