New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-safely-set-inner-html

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-safely-set-inner-html

As the name suggests, this component is the opposite of React's ominous sounding dangerouslySetInnerHTML DOM attribute.

  • 2.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-77.78%
Maintainers
0
Weekly downloads
 
Created
Source

react-safely-set-inner-html

As the name suggests, this component is the opposite of React's ominous sounding dangerouslySetInnerHTML DOM attribute. Under the hood, this component first uses htmlparser2 to parse the html input, then traverses the object tree, transform the data to React props, and then finally call the top-level API React.createElement function to safely render the output.

Usage

import ReactSafelySetInnerHTML from 'react-safely-set-inner-html';

const html = `<p class="text">
  HTML string to set within React using the <a class="link" href="https://github.com/jzhobes/react-safely-set-inner-html">ReactSafelySetInnerHTML</a> component.
</p>`;

return (
  <ReactSafelySetInnerHTML>
    {html}
  </ReactSafelySetInnerHTML>
);

By default, the following tags are allowed:

[
  'div', 'span', 'a', 'b', 'i', 'strong', 'small', 'table', 'thead', 'tbody', 'tfoot',
  'th', 'td', 'tr', 'td', 'caption', 'colgroup', 'col', 'video', 'audio', 'source',
]

You can override the default tags by specifying a custom allowedTags prop:

import ReactSafelySetInnerHTML from 'react-safely-set-inner-html';

return (
  <ReactSafelySetInnerHTML allowedTags={['div', 'span', 'p']}>
    {html}
  </ReactSafelySetInnerHTML>
);

Alternatively, the excludedTags prop blacklists tags, but all other tags not specified in the list will be allowed and the allowedTags prop will be ignored.

import ReactSafelySetInnerHTML from 'react-safely-set-inner-html';

return (
  <ReactSafelySetInnerHTML excludedTags={['iframe', 'form']}>
    {html}
  </ReactSafelySetInnerHTML>
);

String substitutions are supported via the substitutionMap prop with keys wrapped in curly braces. Keys without a match will be ignored.

import ReactSafelySetInnerHTML from 'react-safely-set-inner-html';

return (
  <ReactSafelySetInnerHTML substitutionMap={{name: 'Bob'}}>
    {`Hello, {name}`}
  </ReactSafelySetInnerHTML>
);

Keywords

FAQs

Package last updated on 29 Jul 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