Socket
Socket
Sign inDemoInstall

react-html-string

Package Overview
Dependencies
14
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-html-string

A React declarative component for converting HTML strings into React components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents or `Custom Components`.


Version published
Weekly downloads
612
decreased by-7.69%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

React HTML String 🚀

A React declarative component for converting HTML strings into React components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents or Custom Components.

Install

$ npm install react-html-string

or

$ yarn add react-html-string

How to use

import HTMLString from 'react-html-string';

const html = `
  <div>
    <h1>
      Hello from
      <a
        href="https://github.com/MomenSherif/react-html-string"
        target="_blank"
        >React HTML String</a
      >
    </h1>
    <hr />
    <ul>
      <li>Render HTML string safely</li>
      <li>Provide <b>Custom Components</b> if needed</li>
    </ul>
    <p>Don't forget to ⭐️ the project</p>
  </div>
`;

export default function App() {
  return (
    <div>
      <h1>Hello, React!</h1>
      <HTMLString html={html} />
    </div>
  );
}

With Custom Components

import HTMLString from 'react-html-string';

import Heading from './components/Heading';

const components = {
  a: props => <a {...props} style={{ color: 'red' }} />,
  h1: Heading,
};

const html = `
  <div>
    <h1>
      Hello from
      <a
        href="https://github.com/MomenSherif/react-html-string"
        target="_blank"
        >React HTML String</a
      >
    </h1>
    <hr />
    <ul>
      <li>Render HTML string safely</li>
      <li>Provide <b>Custom Components</b> if needed</li>
    </ul>
    <p>Don't forget to ⭐️ the project</p>
  </div>
`;

export default function App() {
  return (
    <div>
      <h1>Hello, React!</h1>
      <HTMLString html={html} components={components} />
    </div>
  );
}

TypeScript example

Each component is strongly typed with the equivalent dom node type.

import HTMLString, { Components } from 'react-html-string';

import Heading from './components/Heading';

const components: Components = {
  a: props => <a {...props} style={{ color: 'red' }} />,
  h1: Heading,
};

const html = `
  <div>
    <h1>
      Hello from
      <a
        href="https://github.com/MomenSherif/react-html-string"
        target="_blank"
        >React HTML String</a
      >
    </h1>
    <hr />
    <ul>
      <li>Render HTML string safely</li>
      <li>Provide <b>Custom Components</b> if needed</li>
    </ul>
    <p>Don't forget to ⭐️ the project</p>
  </div>
`;

export default function App() {
  return (
    <div>
      <h1>Hello, React!</h1>
      <HTMLString html={html} components={components} />
    </div>
  );
}

Keywords

FAQs

Last updated on 22 May 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc