npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

streact

v2.0.1

Published

Convert text/HTML strings to React

Downloads

7

Readme

streact

Sometimes you need to inject a string of text containing HTML into your React app. This may come from a CMS or a localization config file or some other source. React provides an intentionally rather cumbersome way of doing this with dangerouslySetInnerHTML. The problem with dangerouslySetInnerHTML is you must use it on an HTML element and sometimes that is not what you want.

streact lets you convert strings containing HTML into valid React code that you can use anywhere. You can use it directly as a child of a component or pass it as a prop, no HTML wrapper required.

<SomeComponent>
  {streact("A <em>string</em> with <abbr title='HyperText Markup Language'>HTML</abbr>!")}
</SomeComponent>
<SomeComponent
  someProp={streact("Styles work <em style='background-color: #0f0; color: #f00;'>too.</em>")}
/>

Installation

npm i streact

HTML Attributes

Most attributes in your HTML should just work, however there are some where the name or case type is different between HTML and React. HTML attributes should be all lower case while React expects camel case. HTML uses class while React expects className. HTML uses for while React expects htmlFor.

streact will convert class to className but the rest are up to you. You can pass an options argument to streact which is an object with an attributes property. This takes a object with a mapping of the HTML attribute name to the React attribute name.

{streact("<a href='https://example.com' tabindex='0'>Click Me</a>", {
    attributes: {
        tabindex: 'tabIndex'
    }
})}

The value for the React property name can also be a function which will get passed the HTML attribute name and value. It should return an object with the attribute name and value. This is useful for Boolean attributes.

{streact("You can't <button disabled>Click Me</button> because I'm disabled", {
    attributes: {
        disabled: () => ({ disabled: true })
    }
})}

Replacements

streact also supports string replacements. Just include a replacements object in the options object with the string(s) to replace and the new value(s). The replacement string can be anything you want.

{streact("<a href='$url' tabindex='0'>LINK_TEXT</a>", {
    attributes: {
        tabindex: 'tabIndex'
    },
    replacements: {
        LINK_TEXT: 'Click Me',
        $url: 'https://example.com'
    }
})}

Sanitization

streact does not do any sanitization of the strings it is converting. It's always a good idea to use something like DOMPurify to sanitize any external HTML before injecting it into your code. After all, there is a reason the official way of injecting strings of HTML into a React component is called dangerouslySetInnerHTML: It can be dangerous!