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-element-portal

v2.0.1

Published

Render a React component inline, but target a DOM element (or elements) by id or selector.

Downloads

81

Readme

React Element Portal

travis

Render a React component inline, but target a DOM element (or elements) by id or selector.

Why?

If you're making a shiny new React app where you use React everywhere, for every page, and for the entirety of every page, then you probably don't need this. But if you live in an imperfect world, where you have a server-generated header/footer or some static blog pages, or anything else not fully controlled by React, you can use an ElementPortal to control those things from inside a single root React element.

Install

npm install react-element-portal --save

Usage with vanilla React

Let's say we get this from the server:

<html>
  <body>
    <!-- Header generated by server -->
    <div id="header">
      <a href="/">Home</a>
      <h1>My Sorta Cool App</h1>
      <div id="user">Joe</div>
    </div>
    <!-- Container for React to do its thing -->
    <div id="app"></div>
  </body>
</html>

Even though we don't control the header, we can pretend like parts of it are owned by a single React root element.

import ElementPortal from 'react-element-portal';

ReactDOM.render(
  // Just rendering a single React element.
  <div>
    {/* Use some React to spice up our header. */}
    <ElementPortal id="user">
      <div>
        <Menu>
          <Label>Joe</Label>
          <Items>
            <Item>Upgrade</Item>
            <Item>Settings</Item>
            <Item>Support</Item>
          </Items>
        </Menu>
      </div>
    </ElementPortal>
    {/* And render our main app as a sibling. */}
    <div>
      <h1>My App</h1>
      <p>This is my main app and gets rendered to #app.</p>
    </div>
  </div>,
  document.getElementById('app')
);

You can also use a selector instead of an id.

<ElementPortal selector=".header .user">
  <div>
    ...
  </div>
</ElementPortal>

Additional features

Reset styling

The resetAttributes prop can be used to remove any attributes from the DOM node we are rendering to:

// All styles and classes from the node with id "header" will be cleared
<ElementPortal id="header" resetAttributes={['class', 'style']}>
  <div className="some-other-class">
    ...
  </div>
</ElementPortal>

View property

ElementPortal also accepts an optional view prop that takes a component, to be rendered inside the portal:

<ElementPortal id="header" view={CoolHeaderComponent} />

One advantage of using the view prop is the ability to derive properties from the original DOM node and pass them to the the component.

Let's say our original DOM element already contains some useful data:

<div id="header" data-user-id="26742">
  Joe
</div>

And we would like to render the following component:

const CoolGreeting = ({ userId, name }) => (
  <div>Welcome <a href={`/profile/${userId}`}>{name}!</a></div>
)

By using the mapNodeToProps prop, you can easily pass this data like so:

import dataAttributes from 'data-attributes';

const mapNodeToProps = (node) => ({
  name: node.textContent,
  ...dataAttributes(node)
});

ReactDOM.render(
  <ElementPortal id="header" mapNodeToProps={mapNodeToProps} />,
  document.getElementById('app')
);

Usage as Higher Order Component

ElementPortal can also be used as a HOC:

import { withElementPortal } from 'react-element-portal';
import MyComponent from 'my-component';

const MyComponentWithPortal = withElementPortal(MyComponent);

ReactDOM.render(
  <MyComponentWithPortal id="user" />,
  document.getElementById('app')
);

or composing with other HOC's:

import { withElementPortal } from 'react-element-portal';
import { compose, connect } from 'react-redux';

const MyComponent = (props) => <h1>Hello, {props.name}!</h1>;

const MyComposedComponent = compose(
  withElementPortal,
  connect((state) => ({ name: state.name }))
)(MyComponent);

Passing context to your ElementPortal

Context from your main tree is passed down automatically to your ElementPortal. For example, if you use Redux, the store context will not get lost, and using connect will behave as expected in the children of your ElementPortal.