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-from-html

v0.1.5

Published

> Convert HTML into React components

Downloads

15

Readme

react-from-html

Convert HTML into React components

Intro

react-from-html allows you to convert individual DOM nodes into React components. This means you can use React components in non-React contexts.

You can go from this:

<div class="ShowMore" data-content="Hello, World!"></div>

to this:

<ShowMore content="Hello, World!" />

Quick Start

Install

npm install --save react-from-html

Map your selectors to your components

import React from "react";
import reactFromHTML from "react-from-html";

// Your component
const ShowMore = ({ content }) => (
  <div className="ShowMore" data-content={content} />
);

// Can be anywhere on page. You can also have multiple entry points.
const root = document.querySelector("body");

reactFromHTML(root, {
  // Map all `.ShowMore` elements using a hydrator
  ".ShowMore": async el => <ShowMore content={el.dataset.content} />,
});

API

reactFromHTML(root, hydrators, options)

Params:

  • root: Element - Root element containing your components
  • hydrators: Object - mapping functions
    • key: String - The query selector for the component
    • value: Func - A Hydrator Function, describing how to map this element to a React component (see below).
  • Options: Object
    • extra: Object - additional properties passed to each hydrator
    • getQuerySelector: (key:String): String - a function for defining custom query selectors for each key

hydrator(el, hydrate, extra): ReactNode

Defined by you for each component you want to convert back into React. It maps an Element into a ReactNode.

  • el: Element - the element for this component, as defined by the query selector
  • hydrate(el): Func - a convenience, prebaked version of reactFromHTML used for hydrating child nodes. For example, converting innerHTML into React children.
  • extra: Object - additional params as defined in the initial reactFromHTML() call.

Examples

Basic hydration

This example shows hydration of a basic component, with the single prop content that is assigned to a data attribute.

React

const ShowMore = ({ content }) => (
  <div className="ShowMore" data-content={content} />
);

// <ShowMore content="Hello, World" />

HTML

<body>
  <div class="ShowMore" data-content="Hello, World"></div>
</body>

Hydration

reactFromHTML(document.querySelector("body"), {
  ".ShowMore": async el => <ShowMore content={el.dataset.content} />,
});

Advanced hydration

This example shows hydration of a more complex component, with multiple props, and children which may themselves need hydrating.

React

const Modal = ({ children }) => (
  <div className="Modal">
    <div class="Modal-title">{title}</div>
    <div class="Modal-inner">{children}</div>
  </div>
);

const ShowMore = ({ content }) => (
  <div className="ShowMore" data-content={content} />
);

// <Modal title="My Modal">
//   <ShowMore content="Hello, World">
// </Modal>

HTML

<body>
  <div class="Modal">
    <div class="Modal-title">My Modal</div>
    <div class="Modal-inner">
      <div class="ShowMore" data-content="Hello, World"></div>
    </div>
  </div>
</body>

Hydration

const ModalHydrator = async (el, hydrate) => (
  <Modal title={el.querySelector(".Modal-title").innerHTML}>
    {await hydrate(el.querySelector(".Modal-inner"))}
  </Modal>
);

const ShowMoreHydrator = async el => <ShowMore content={el.dataset.content} />;

reactFromHTML(document.querySelector("body"), {
  ".Modal": ModalHydrator,
  ".ShowMore": ShowMoreHydrator,
});

Acknowledgement

This package is based on the excellent react-from-markup from Simon Andrews.

LICENSE

MIT