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

html-to-react

Package Overview
Dependencies
Maintainers
3
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-to-react

A lightweight library that converts raw HTML to a React DOM structure.

  • 1.7.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
395K
increased by3.94%
Maintainers
3
Weekly downloads
 
Created

What is html-to-react?

The html-to-react npm package is a utility that allows you to convert raw HTML strings into React components. This can be particularly useful for rendering HTML content dynamically in a React application.

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

Basic HTML to React Conversion

This feature allows you to convert a simple HTML string into a React element. The `HtmlToReact.Parser` class is used to parse the HTML string and convert it into a React component.

const HtmlToReact = require('html-to-react');
const htmlToReactParser = new HtmlToReact.Parser();
const html = '<div>Hello, <strong>world</strong>!</div>';
const reactElement = htmlToReactParser.parse(html);

Custom Processing Instructions

This feature allows you to define custom processing instructions to handle specific HTML tags in a custom way. In this example, all `div` elements are given a custom class name.

const HtmlToReact = require('html-to-react');
const htmlToReactParser = new HtmlToReact.Parser();
const processingInstructions = [
  {
    shouldProcessNode: function (node) {
      return node.name && node.name === 'div';
    },
    processNode: function (node, children, index) {
      return React.createElement('div', { key: index, className: 'custom-div' }, children);
    }
  }
];
const html = '<div>Hello, <strong>world</strong>!</div>';
const reactElement = htmlToReactParser.parseWithInstructions(html, () => true, processingInstructions);

Handling Complex HTML Structures

This feature demonstrates the ability to handle more complex HTML structures, such as nested elements. The parser can convert nested HTML elements into nested React components.

const HtmlToReact = require('html-to-react');
const htmlToReactParser = new HtmlToReact.Parser();
const html = '<div><p>Paragraph 1</p><p>Paragraph 2</p></div>';
const reactElement = htmlToReactParser.parse(html);

Other packages similar to html-to-react

Keywords

FAQs

Package last updated on 04 Oct 2023

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