Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html-react-parser

Package Overview
Dependencies
Maintainers
0
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-react-parser

HTML to React parser.

  • 5.1.18
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5M
increased by6.83%
Maintainers
0
Weekly downloads
 
Created

What is html-react-parser?

The html-react-parser package is designed to convert HTML strings into React components. This is particularly useful when you need to dynamically render HTML content in a React application, such as content fetched from a CMS or API that returns HTML. It allows for custom handling of elements, attributes, and can work with server-side rendering.

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

Parsing HTML strings to React Elements

This feature allows you to convert a string of HTML into React elements that can be rendered inside a React component.

import parse from 'html-react-parser';
const html = '<div>Hello World</div>';
const reactElement = parse(html);

Replacing or modifying elements during parsing

This feature allows you to define a 'replace' function in the options object that can modify or replace elements during the parsing process.

import parse, { domToReact } from 'html-react-parser';
const html = '<p id="replace">Replace me</p>';
const options = {
  replace: ({ attribs, children }) => {
    if (attribs && attribs.id === 'replace') {
      return <span>{domToReact(children)}</span>;
    }
  }
};
const reactElement = parse(html, options);

Preserving custom attributes and event handlers

This feature allows you to preserve custom attributes and potentially event handlers when parsing HTML to React elements.

import parse from 'html-react-parser';
const html = '<div onclick="handleClick()">Click me</div>';
const reactElement = parse(html, {
  preserveAttributes: ['onclick']
});

Other packages similar to html-react-parser

Keywords

FAQs

Package last updated on 06 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc