2.0.0 • Published 2 months ago

@rcp/hoc.mount v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@rcp/hoc.mount

NPM version NPM Downloads

The high order component for mounting component

It's useful on modal component which requires should be mounted at other node (e.g. document.body) or requires single rendering instance.

Installation

npm install @rcp/hoc.mount
# or use yarn
yarn add @rcp/hoc.mount

Usage

import mount from '@rcp/hoc.mount'

const Title = mount()(({ props }) => <h2>{props.title}</h2>)

ReactDOM.render(
  <div>
    <Title title="foo" />
    <Title title="bar" />
  </div>,
  window.root
)
// document.body.outerHTML
// <body>
//   <div id="root"></div>
//   <div><h2>bar</h2></div>
// </body>

const TitleComponent = mount({
  createTimeType: 'component',
  attributesGetter: ({ title }) => ({ id: title })
})(({ props }) => <h2>{props.title}</h2>)

ReactDOM.render(
  <div>
    <TitleComponent title="foo" />
    <TitleComponent title="bar" />
    <TitleComponent title="barbar" />
  </div>,
  window.root
)
// document.body.outerHTML
// <body>
//   <div id="root"></div>
//   <div id="foo"><h2>foo</h2></div>
//   <div id="bar"><h2>bar</h2></div>
//   <div id="barbar"><h2>barbar</h2></div>
// </body>

API

Options

  • createTimeType ("decorator" | "class" | "component") (optional, default 'class')

    Create mount center in which time.

    • decorator: create at calling mount() time.
    • class: create at calling mount()(Component) time.
    • component: create at rendering mount()(Component) time.
  • mountNodeGetter ((props) => HTMLElement) (optional, default () => document.body)

  • attributesGetter ((props) => any) (optional, default () => {})

Related

Authors

This library is written and maintained by imcuttle, moyuyc95@gmail.com.

License

MIT

2.0.0

2 months ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.17

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.12

5 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago