Skip to content

bobderrico80/react-html-parser

Repository files navigation

@bobderrico/react-html-parser

A lightweight parser to convert an HTML string into React elements

NPM JavaScript Style Guide

Install

npm install --save @bobderrico/react-html-parser

Usage

import React from 'react'
import { parseHTML } from '@bobderrico/react-html-parser'

const html = '<h1><strong>Hello</strong> world!</h1>'

const Component = () => {
  return <div>{parseHTML(html)}</div>
}

The above <Component /> would render as so:

<div>
  <h1><strong>Hello</strong> world!</h1>
</div>

For more examples, see https://bobderrico80.github.io/react-html-parser/

Inspiration

There are several other solutions out there that solve a similar problem, html-react-parser, react-html-parser, and html-to-react to name a few. I wanted to challenge myself to build one of my own with a more familiar API, especially in regards to the replacer function, as that function is called with an HTMLElement.

This package relies on DOMParser and therefore currently only works in the browser.

License

MIT © bobderrico80

About

A lightweight parser to convert an HTML string into React elements

Resources

Stars

Watchers

Forks

Packages

No packages published