🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

react-html-string

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

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`.

0.1.1
latest
Source
npm
Version published
Weekly downloads
504
-27.9%
Maintainers
1
Weekly downloads
 
Created
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

react

FAQs

Package last updated on 22 May 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