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-isomorphic-portals

v0.1.0

Published

Create react portals on the server

Downloads

15

Readme

react-isomorphic-portals

Create react portals on the server

Installation

Using npm:

$ npm install --save react-isomorphic-portals

Using yarn:

$ yarn add react-isomorphic-portals

Usage

On the server, create a PortalCollector and pass it as the value prop of a PortalCollectorProvider:

import { PortalCollector, PortalCollectorProvider } from 'react-isomorphic-portals';
import { renderToString } from 'react-dom/server';

// Where your server renders the application:

const collector = new PortalCollector();

const app = renderToString(
  <PortalCollectorProvider value={collector}>
    <App />
  </PortalCollectorProvider>
);

To create a portal, import createPortal from this module.

import { createPortal } from 'react-isomorphic-portals';

const Modal = () => 'This is a modal component';

const ModalContainer = () => createPortal(<Modal />, 'modal-root');

Note that this module's createPortal function receives an element ID as its second argument, not a DOM element.

The PortalCollector's renderPortalsToStaticMarkup method returns an object containing all rendered portals. These should then be added to the HTML. This can be done with whatever template engine you want.

Here's an example using template strings:

const portals = collector.renderPortalsToStaticMarkup();

const html = `
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Page Title</title>
</head>
<body>
  <div id="root">${app}</div>
  <div id="modal-root">${portals['modal-root']}</div>
</body>
</html>
`;

Another thing you'll need to do is remove the server rendered portals on the client before the first render. This can be done like this:

document.getElementById('modal-root').childNodes.forEach(node => node.remove());

This is needed because React will ignore the server renderer portals and add the portals another time.

API

createPortal(children: React$Node, elementId: string): React$Node

The first argument (children) is any renderable React child, such as an element, string, or fragment. The second argument (elementId) is the ID of a DOM element.

PortalCollector

Collects portals on the server for rendering to static markup.

`renderPortalsToStaticMarkup(): { [elementId: string]: Array }

Returns an object whose keys are the DOM element IDs passed to createPortal and whose values are arrays of renderer portals.

These arrays should be added to the HTML on the server.

PortalCollectorProvider

Provides a PortalCollector context that is used when calling createPortal on the server.

Contributing

Please feel free to submit any issues or pull requests.

License

MIT