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

@elliottsj/react-render-html

v0.2.3

Published

No more dangerouslySetInnerHTML, render HTML as React element.

Downloads

8

Readme

react-render-html travis-ci

No more dangerouslySetInnerHTML, render HTML as React element.

How it works

It renders a provided HTML string into a React element.

import renderHTML from 'react-render-html';

renderHTML("<a class='github' href='https://github.com'><b>GitHub</b></a>")
// => React Element
//    <a className="github" href="https://github.com"><b>GitHub</b></a>

It may be used in the render method in a React component:

let App = React.createClass({
  render() {
    return (
      <div className='app'>
        {renderHTML(someHTML)}
      </div>
    );
  }
});

Or just by itself

ReactDOM.render(renderHTML(someHTML), document.getElementById('app'));

If a provided HTML contains several top-level nodes, the function will return an array of React elements.

renderHTML('<li>hello</li><li>world</li>');
// => [React Element <li>hello</li>, React Element <li>world</li>]

Custom renderers ("Middleware")

Pass a function as the 2nd argument to renderHTML to customize how nodes are rendered:

function middleware(renderNode) { /* ... */ }

renderHTML('<li>hello</li><li>world</li>', middleware);

The middleware function should have the signature

type Middleware = (renderNode: NodeRenderer) => (next: NodeRenderer) => NodeRenderer
type NodeRenderer = (node: ASTNode.<Element>, key: String) => ReactElement

Where next is the next renderer in the middleware chain, renderNode is the entire chain, and node and key correspond to the current node being rendered.

For example, to replace the href attribute of all <a> elements, use:


function replaceHref(renderNode) {
  return next => (node, key) => {
    const element = next(node, key);
    if (node.tagName === 'a') {
      return React.cloneElement(element, {
        href: 'https://example.com'
      });
    }
    return element;
  };
}

const htmlElement = renderHTML('<p><a class="hello" href="https://github.com">hihi</a></p>', replaceHref);
console.log(renderToStaticMarkup(htmlElement));
// <p><a class="hello" href="https://example.com">hihi</a></p>

Custom renders are composable: using applyMiddleware, simply call next from within a renderer to get the resulting ReactElement from subsequent renderers, and call renderNode if you need to render a node from scratch using the entire renderer chain (e.g. child nodes). For example:

import renderHTML, {
  applyMiddleware
} from 'react-render-html';

const replaceHref = renderNode => next => (node, key) => {
  const element = next(node, key);
  if (node.tagName === 'a') {
    return React.cloneElement(element, {
      href: 'https://example.com'
    });
  }
  return element;
};
const replacePs = renderNode => next => (node, key) => {
  if (node.tagName === 'p') {
    return React.createElement(node.tagName, {}, 'Redacted');
  }
  return next(node, key);
};
const addLi = renderNode => next => (node, key) => {
  const element = next(node, key);
  if (node.tagName === 'ul') {
    return React.cloneElement(
      element,
      {},
      ...node.childNodes.map(renderNode),
      React.createElement('li', {}, 'One more')
    );
  }
  return element;
};
const htmlElement = renderHTML(
  '<ul>' +
    '<li><a class="hello" href="https://github.com">hihi</a></li>' +
    '<li><p><b>hello</b>world</p><p>react</p></li>' +
  '</ul>',
  applyMiddleware(replaceHref, replacePs, addLi)
);

const htmlElement = renderHTML('<p><a class="hello" href="https://github.com">hihi</a></p>', replaceHref);
console.log(renderToStaticMarkup(htmlElement));
// <ul><li><a class="hello" href="https://example.com">hihi</a></li><li><p>Redacted</p><p>Redacted</p></li><li>One more</li></ul>

Install

Install with NPM:

npm i --save react-render-html

Import with CommonJS or whatever:

const {
  default: renderHTML,
  applyMiddleware
} = require('react-render-html');

// OR

import renderHTML, { applyMiddleware } from 'react-render-html';

A bug!

When a bug is found, please report them in Issues.

Also, any form of contribution(especially a PR) will absolutely be welcomed :beers:

License

MIT