Git Product home page Git Product logo

react-portalize's Introduction


react-portalize

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i react-portalize

A convenient component for injecting elements into React Portals via DOM selectors. You may optionally choose to render your portals on the server side as well :)

Quick Start

import Portalize from 'react-portalize'


// renders 'Hello' into <div id='portals'/>
<Portalize container='#portals'>
  Hello
</Portalize>

// renders 'Hello' into all instances of <div class='portals'/>
<Portalize container='.portals'>
  Hello
</Portalize>

// renders 'Hello' into all instances of <section/>
<Portalize container='section'>
  Hello
</Portalize>

// renders 'Hello' into all instances of <div data-portalize="why not"/>
<Portalize container='div[data-portalize="why not"]'>
  Hello
</Portalize>

API

<Portalize>

  • container: string

    • The DOM selector you'd like to render your children into
    • default #portals
  • children: React.ReactNode

    • Anything React can render
  • server: boolean

    • If you want to skip rendering this component on the server side you can do so with the server={false} flag. You don't need to worry about turning this off if you aren't rendering on the server with renderPortalsToString
    • default true
  • providers {provider: React.ReactProvider, value: any}[]

    • Critically, this component will not work with portals in SSR that utilize context out of the box. This is because the children are never rendered into the virtual DOM tree on the server side. This hacky approach fixes that problem by creating your portals with <Provider value={}/> components wrapped around them. The providers supplied in the array are reduced from the right, so [a, b, c] renders as <a><b><c/></b></a>.
      <Portalize providers={[{provider: YourProvider, value: YourProviderValue}]}>
        <YourConsumer>{(value) => JSON.stringify(value)}</YourConsumer>
      </Portalize>

react-portalize/server

renderPortalsToString(html <string>)

  • Injects portals created within your App into their respective containers. You can provide either your entire <!DOCTYPE html> string here or alternatively just the React root generated by ReactDOMServer.renderToString().

SSR

Example with React root as the entry point

import {renderPortalsToString} from 'react-portalize/server'

function serverRenderer req, res, next() {
  const page = renderPortalsToString(
    ReactDOMServer.renderToString(<App/>)
  )

  res.set('Content-Type', 'text/html').send(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Hello world app</title>
    </head>
    <body>
      <div id="⚛️">${page}</div>
    </body>
    </html>
  `)
}

Example with HTML root as the entry point

import {renderPortalsToString} from 'react-portalize/server'

function serverRenderer(req, res, next) {
  const page = ReactDOMServer.renderToString(<App />)

  res.set('Content-Type', 'text/html').send(
    renderPortalsToString(`
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <title>Hello world app</title>
      </head>
      <body>
        <div id="⚛️">${page}</div>
      </body>
      </html>
    `)
  )
}

Note

You will receive a warning message in the console from ReactDOM.hydrate() in "development" akin to Did not expect server HTML to contain the text node "Hello" in <div>.. This is because ReactDOM.hydrate() does not expect your portals to be rendered into the App. You can safely ignore this warning.

LICENSE

MIT

react-portalize's People

Contributors

jaredlunde avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

entraptaj

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.