@artsy/react-html-parser

中文文档
3.0.2发布于 2019/10/15MITCommonJS零依赖近 30 天下载 2.1万

Parse HTML into React components

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. API
  4. function ReactHtmlParser(html, [options])
  5. function convertNodeToElement(node, index, transform)

React HTML Parser

NOTE: This Artsy fork replaces the htmlparser2 dependency with the DOMParser API provided by modern browsers. As such, the nodes that you get to operate on have a slightly different API.

A utility for converting HTML strings into React components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents.

Try the Live Demo

Travis branch Coveralls npm Downloads David

Install

npm install @artsy/react-html-parser
# or
yarn add @artsy/react-html-parser

Usage

import React from 'react';
import ReactHtmlParser, {
  processNodes,
  convertNodeToElement
} from 'react-html-parser';

class HtmlComponent extends React.Component {
  render() {
    const html = '<div>Example HTML string</div>';
    return <div>{ReactHtmlParser(html)}</div>;
  }
}

API

function ReactHtmlParser(html, [options])

Takes an HTML string and returns equivalent React elements

Usage

import ReactHtmlParser from 'react-html-parser';

Arguments

  • html: The HTML string to parse
  • options: Options object
    • transform (function): Transform function that is applied to every node
    • preprocessNodes (function): Pre-process the nodes generated by DOMParser

Transform Function

The transform function will be called for every node that is parsed by the library.

function transform(node, index)

Arguments
  • node: The node being parsed. This is a plain Node / Element as otherwise generated by the browser.

Return Types

return null Returning null will prevent the node and all of it's children from being rendered.

function transform(node) {
  // do not render any <span> tags
  if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'SPAN') {
    return null;
  }
}

return undefined If the function does not return anything, or returns undefined, then the default behaviour will occur and the parser will continue was usual.

return React element React elements can be returned directly

import React from 'react';
function transform(node) {
  if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'B') {
    return <div>This was a bold tag</div>;
  }
}

preprocessNodes Function

Allows pre-processing the nodes generated from the html by DOMParser before being passed to the library and converted to React elements.

function preprocessNodes(nodes)

Arguments
  • nodes: The entire node tree generated by DOMParser.
Return type

The preprocessNodes function should return a valid DOMParser node tree.

function convertNodeToElement(node, index, transform)

Processes a node and returns the React element to be rendered. This function can be used in conjunction with the previously described transform function to continue to process a node after modifying it.

Usage

import { convertNodeToElement } from 'react-html-parser';

Arguments

  • node: The node to process
  • index (number): The index of the node in relation to it's parent
  • transform: The transform function as described above
import { convertNodeToElement } from 'react-html-parser';
function transform(node, index) {
  // convert <ul> to <ol>
  if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'UL') {
    node.name = 'ol';
    return convertNodeToElement(node, index, transform);
  }
}

版本与发布记录

@artsy/react-html-parser 共发布 3 个版本,首次发布于 2019/10/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2019/10/157 年前
3.0.12019/10/147 年前
3.0.02019/10/127 年前

关于 @artsy/react-html-parser 的常见问题

@artsy/react-html-parser 是什么?

Parse HTML into React components

如何安装 @artsy/react-html-parser?

在项目目录运行 npm install @artsy/react-html-parser,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^0.14.0 || ^15.0.0 || ^16.0.0-0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@artsy/react-html-parser 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/artsy__react-html-parser 是否存在。

@artsy/react-html-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@artsy/react-html-parser 可以商用吗?

@artsy/react-html-parser 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@artsy/react-html-parser 的下载量有多少?

根据 npm 官方下载统计,@artsy/react-html-parser 在 2026/10/07 – 2026/11/05 这 30 天内被下载 20,647 次,日均约 688 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@artsy/react-html-parser 还在维护吗?

最新版本 3.0.2 发布于 2019/10/15(7 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/10/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。