# react-html-renderer

> React component that renders an HTML string as a React component tree

Latest version **0.3.3** (published 2021-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-html-renderer
pnpm add react-html-renderer
yarn add react-html-renderer
bun add react-html-renderer
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2021-10-25 |
| First published | 2019-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 14.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Angelo Ashmore |
| Maintainers | angeloashmore, asyarb |
| Keywords | react, component, html |

## Links

- npm: https://www.npmjs.com/package/react-html-renderer
- Repository: https://github.com/angeloashmore/react-html-renderer
- npm.io page: https://npm.io/package/react-html-renderer

## Dependencies (6)

- [eslint](https://npm.io/package/eslint.md) ^6.2.2
- [babel-eslint](https://npm.io/package/babel-eslint.md) ^10.0.3
- [html-react-parser](https://npm.io/package/html-react-parser.md) ^0.9.1
- [eslint-plugin-jest](https://npm.io/package/eslint-plugin-jest.md) ^22.15.2
- [eslint-plugin-react](https://npm.io/package/eslint-plugin-react.md) ^7.14.3
- [react-test-renderer](https://npm.io/package/react-test-renderer.md) ^16.9.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.3.3 (latest) — 2021-10-25
- 0.3.2 — 2019-09-06
- 0.3.1 — 2019-09-06
- 0.3.0 — 2019-08-27
- 0.2.1 — 2019-06-14
- 0.1.1 — 2019-04-05
- 0.1.0 — 2019-04-05

## README

# react-html-renderer

React component that renders an HTML string as a React component tree.

**Note**: This component uses [`html-react-parser`][html-react-parser] under the
hood but makes no promises about changing the underlying library in a future
release.

## Status

[![npm version](https://flat.badgen.net/npm/v/react-html-renderer)](https://www.npmjs.com/package/react-html-renderer)
[![Build Status](https://flat.badgen.net/travis/angeloashmore/react-html-renderer)](https://travis-ci.com/angeloashmore/react-html-renderer)

## Install

```sh
npm install --save react-html-renderer
```

## Example

```jsx
import React from 'react'
import HTMLRenderer from 'react-html-renderer'

// Components to which elements are mapped
import Heading from './Heading'
import Subheading from './Subheading'
import Link from './Link'

// HTML to render as React components
const html = `
  <h1>React</h1>
  <h2>A JavaScript library for building user interfaces</h2>
  <p>
    <a href="#">Get Started</a>
  </p>
`

// Note that default props can be set using the following pattern:
//
//   `props => <Comp foo="bar" {...props} />`
//
const App = () => (
  <HTMLRenderer
    html={html}
    components={{
      h1: props => <Heading color="red" {...props} />,
      h2: Subheading,
      a: Link,
    }}
  />
)
```

`HTMLRenderer` will render something that looks like the following:

```jsx
;[
  <Heading color="red">React</Heading>,
  <Subheading>A JavaScript library for building user interfaces</Subheading>,
  <p>
    <Link to="#">Get Started</Link>
  </p>,
]
```

## Component overrides

`HTMLRenderer` supports overriding components provided in the `components` prop
as needed. This can be utilized to create a reusable `HTMLRenderer` with a
default set of components throughout your project.

```jsx
// src/components/HTML.js

import { Heading, Subheading, Link } from 'src/components'

export const HTML = props => (
  <HTMLRenderer
    components={{
      h1: props => <Heading color="red" {...props} />,
    }}
    {...props}
  />
)
```

The `HTML` component could be used by passing it an HTML string.

```js
// src/pages/index.js

import { HTML } from 'src/components'

export const IndexPage = ({ html }) => <HTML html={html} />
```

This will render `H1` elements with **red text**.

If individual components need to be overridden, you can provide a mapping using
the `componentOverrides` prop.

```js
// src/pages/index.js

import { HTML } from 'src/components'

export const IndexPage = ({ html }) => (
  <HTML
    html={html}
    componentOverrides={{
      h1: Comp => props => <Comp {...props} color="blue" />,
    }}
  />
)
```

This will render `H1` elements with **blue text**.

Note that `Comp` is the `Heading` component defined in the original `components`
prop. This allows you to keep the existing component and modify it as needed.
Alternatively, you could disregard `Comp` and return a completely different
component.

## Props

| Name                     | Type                                 | Description                                                                                                                                   |
| ------------------------ | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| **`html`**               | `PropTypes.string`                   | HTML to render.                                                                                                                               |
| **`components`**         | `PropTypes.objectOf(PropTypes.node)` | An object mapping an HTML element type to anything React can render (numbers, strings, elements, etc.).                                       |
| **`componentOverrides`** | `PropTypes.objectOf(PropTypes.func)` | An object mapping an HTML element type to a function that returns anything React can render. See [Component overrides](#component-overrides). |

## Similar packages

- [markdown-react-renderer][markdown-react-renderer]

[html-react-parser]: https://github.com/remarkablemark/html-react-parser
[markdown-react-renderer]: https://github.com/asyarb/markdown-react-renderer

---
_Source: https://npm.io/package/react-html-renderer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
