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

react-dynamic-html

v3.0.1

Published

Like `dangerouslySetInnerHTML` but with simple old-school template substitution, where values can be React components.

Downloads

87

Readme

react-dynamic-html

Like dangerouslySetInnerHTML but with simple old-school template substitution, where values can be React components.

Because sometimes you just need to work with HTML from an older system or CMS.

  • Tiny <1KB client-side bundle, no dependencies necessary
  • Template values can be dynamic React components
  • Universal, supports server side rendering (SSR)
  • Components get the correct context from above
  • Template updates even preserve component state!

Example

import Template from "react-dynamic-html";

/**
 * This app shows off rendering interactive React components and
 * dynamic values into an HTML string.
 */
class App extends React.Component {
  state = {
    name: "Alice"
  };

  toggleName = () => {
    this.setState(state => ({
      name: state.name === "Alice" ? "Bob" : "Alice"
    }));
  };

  render() {
    return (
      <Template
        string={`
          <h1>Hey, {name}!</h1>
          <p>Click here: {button}</p>
        `}
        values={{
          name: this.state.name,
          button: <button onClick={this.toggleName}>Toggle</button>
        }}
      />
    );
  }
}

API

Template

Props

The DOM element type in which to render the entire template.

The DOM element type in which to render React element values by default. To override the tag for individual values, use valueTags.

Whether or not to escape values inserted into the HTML.

The template HTML string.

The string or RegExp that specifies the variable substitution syntax. Each instance will be replaced. The second capture group should be the name of the variable.

An object mapping variable names (used in the template string) to their values. React element values will be rendered into a placeholder node.

The DOM element type in which to render specific React elements that appear in values. Only React elements are wrapped.

Other Solutions

This library should pretty much be a last resort. Here’s some advice:

If you can preprocess the HTML into a simple AST and serve that to your app instead, do that. Then you can easily translate nodes into React.createElement calls. Everything will be easier and React will be happy. (This library does exactly that during server-side rendering in order to render with the correct context, but doing this client-side requires large dependencies.)

If your HTML authors are technical and are editing raw HTML, you can avoid parsing {placeholders} and instead come up with some fancy data- attributes to indicate how to transform certain elements in React components (even allowing them to pass simple props). Then you just need to find and replace these nodes after mounting. (As above, you’ll still need an HTML parser on the server.)

If the HTML comes from a WYSIWYG editor where special {placeholder} syntax is more accessible, and you just need to stick React components (like interactive buttons) and text in a few places, use this library.