🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More

react-html-parser

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-html-parser

Parse HTML into React components

2.0.2
latest
Version published
Weekly downloads
243K
0.95%
Maintainers
1
Weekly downloads
 
Created

What is react-html-parser?

The react-html-parser npm package is used to parse HTML strings into React components. This allows developers to dynamically render HTML content within their React applications.

What are react-html-parser's main functionalities?

Basic HTML Parsing

This feature allows you to parse a basic HTML string and render it as React components. The HTML string is converted into React elements that can be rendered in the application.

const React = require('react');
const ReactDOM = require('react-dom');
const ReactHtmlParser = require('react-html-parser');

const htmlString = '<div><h1>Hello, World!</h1><p>This is a paragraph.</p></div>';
const App = () => (
  <div>
    {ReactHtmlParser(htmlString)}
  </div>
);

ReactDOM.render(<App />, document.getElementById('root'));

Handling Inline Styles

This feature allows you to parse HTML strings that include inline styles. The styles are preserved and applied to the rendered React components.

const React = require('react');
const ReactDOM = require('react-dom');
const ReactHtmlParser = require('react-html-parser');

const htmlString = '<div style="color: red;"><h1>Hello, World!</h1><p>This is a paragraph with inline styles.</p></div>';
const App = () => (
  <div>
    {ReactHtmlParser(htmlString)}
  </div>
);

ReactDOM.render(<App />, document.getElementById('root'));

Custom Transformations

This feature allows you to apply custom transformations to specific HTML elements during the parsing process. In this example, the <h1> tag is transformed to have a blue color.

const React = require('react');
const ReactDOM = require('react-dom');
const ReactHtmlParser = require('react-html-parser');

const htmlString = '<div><h1>Hello, World!</h1><p>This is a paragraph.</p></div>';

const transform = (node) => {
  if (node.type === 'tag' && node.name === 'h1') {
    return <h1 style={{ color: 'blue' }}>{node.children[0].data}</h1>;
  }
};

const App = () => (
  <div>
    {ReactHtmlParser(htmlString, { transform })}
  </div>
);

ReactDOM.render(<App />, document.getElementById('root'));

Other packages similar to react-html-parser

FAQs

Package last updated on 29 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts