For TypeScript projects, you may need to check that <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>3 is an instance of domhandler's <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>4:
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')4
If you're having issues take a look at our Create React App example.
replace element and children
Replace the element and its children (see demo):
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')5
HTML output:
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')6
replace element attributes
Convert DOM attributes to React props with <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>5:
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')7
HTML output:
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')8
replace and remove element
Exclude an element from rendering by replacing it with <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>6:
const parse = require('html-react-parser'); parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')9
HTML output:
npm install html-react-parser --save0
library
The <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>7 option specifies the UI library. The default library is React.
To use Preact:
npm install html-react-parser --save1
Or a custom library:
npm install html-react-parser --save2
htmlparser2
<!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>8 options do not work on the client-side (browser) and only works on the server-side (Node.js). By overriding <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>8 options, universal rendering can break.
Default can be overridden in >=0.12.0.
To enable :
npm install html-react-parser --save3
trim
By default, whitespace is preserved:
npm install html-react-parser --save4
But certain elements like // ES Modules import parse from 'html-react-parser'; // CommonJS const parse = require('html-react-parser');1 will strip out invalid whitespace:
npm install html-react-parser --save5
To remove whitespace, enable the <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>0 option:
npm install html-react-parser --save6
However, intentional whitespace may be stripped out:
npm install html-react-parser --save7
Migration
v3.0.0
domhandler has been upgraded to v5 so some parser options like // ES Modules import parse from 'html-react-parser'; // CommonJS const parse = require('html-react-parser');3 have been removed.
Also, it's recommended to upgrade to the latest version of typescript.
v2.0.0
Since v2.0.0, Internet Explorer (IE) is no longer supported.
v1.0.0
TypeScript projects will need to update the types in v1.0.0.
For the yarn add html-react-parser8 option, you may need to do the following:
npm install html-react-parser --save8
Since v1.1.1, Internet Explorer 9 (IE9) is no longer supported.
FAQ
Is this XSS safe?
No, this library is not XSS (cross-site scripting) safe. See #94.
Does invalid HTML get sanitized?
No, this library does not sanitize HTML. See #124, #125, and #141.
Are yarn add html-react-parser9 tags parsed?
Although yarn add html-react-parser9 tags and their contents are rendered on the server-side, they're not evaluated on the client-side. See #98.
Attributes aren't getting called
The reason why your HTML attributes aren't getting called is because inline event handlers (e.g., // ES Modules import parse from 'html-react-parser'; // CommonJS const parse = require('html-react-parser');7) are parsed as a string rather than a function. See #73.
Parser throws an error
If the parser throws an erorr, check if your arguments are valid. See .
Is SSR supported?
Yes, server-side rendering on Node.js is supported by this library. See demo.
Elements aren't nested correctly
If your elements are nested incorrectly, check to make sure your HTML markup is valid. The HTML to DOM parsing will be affected if you're using self-closing syntax (// ES Modules import parse from 'html-react-parser'; // CommonJS const parse = require('html-react-parser');8) on non-void elements:
npm install html-react-parser --save9
See #158.
Don't change case of tags
Tags are lowercased by default. To prevent that from happening, pass the :
yarn add html-react-parser0
Warning: By preserving case-sensitivity of the tags, you may get rendering warnings like:
yarn add html-react-parser1
See #62 and example.
TS Error: Property 'attribs' does not exist on type 'DOMNode'
The TypeScript error occurs because // ES Modules import parse from 'html-react-parser'; // CommonJS const parse = require('html-react-parser');9 needs be an instance of domhandler's <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>4. See or #199.
Can I enable <!-- HTMLReactParser depends on React --> <script src="//unpkg.com/react@18/umd/react.production.min.js"></script> <script src="//unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script> <script> window.HTMLReactParser(/* string */); </script>0 for certain elements?
Yes, you can enable or disable for certain elements using the option. See #205.
Webpack build warnings
If you see the Webpack build warning:
yarn add html-react-parser2Then update your Webpack config to:
yarn add html-react-parser3
See #238 and #213.
TypeScript error
If you see the TypeScript error:
yarn add html-react-parser4Then upgrade to the latest version of typescript. See #748.
Performance
Run benchmark:
yarn add html-react-parser5
Output of benchmark run on MacBook Pro 2017:
yarn add html-react-parser6Run Size Limit:
yarn add html-react-parser7
Contributors
Code Contributors
This project exists thanks to all the people who contribute. [Contribute].
Financial Contributors
Become a financial contributor and help us sustain our community. [Contribute]
Individuals
Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
Enterprise
Available as part of the Tidelift Subscription.
The maintainers of html-react-parser and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.