react-jumpgate
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

react-jumpgate

A generic implementation of portals for React, for platforms where ReactDOM is not available (eg. this could be used in a React Native application to add buttons to the toolbar from any component in the tree)

import createJumpgate from 'react-jumpgate';

// A jumpgate is made of 3 components: an anchor, a consumer and a provider
const { Anchor, Consumer, Provider } = createJumpgate();

const App = () => (
    <div class="app">
        {/* Place the anchor near the root of your component tree */}
        <Anchor>
            <div class="header">
                {/* The consumer acts as a placeholder for the component you want to teleport */}
                <Consumer />
            </div>
            <div class="content">
                {/* The provider will send its children through the jumpgate to the consumer */}
                <Provider>
                    <h1>Title</h1>
                </Provider>
            </div>
        </Anchor>
    </div>
);

/* <App /> will render as
    <div class="app">
        <div class="header">
            <h1>Title</h1>
        </div>
        <div class="content" />
    </div>
*/

Readme

Keywords

none

Package Sidebar

Install

npm i react-jumpgate

Weekly Downloads

34

Version

1.2.0

License

MIT

Unpacked Size

11.3 kB

Total Files

4

Last publish

Collaborators

  • leops