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

@s7n/react-mounter

v1.0.3

Published

react-mounter is a separate component mounting approach in the React system

Downloads

7

Readme

ENGLISH | 中文文档

React Mounter

React Mounter 是基于 React 体系的一种分离式组件挂载方案。

它可以有效地解决组件挂载受到组件层级结构限制的问题,其能力有点类似于 createPortal 但是它的语法更加简洁。

npm version npm downloads

安装

@s7n/react-mounter 安装为依赖,就可以在你的 React App 中使用了:

# If you use npm:
npm install @s7n/react-mounter 

# If you use yarn:
yarn add @s7n/react-mounter

使用

MountProvider 组件下声明要被挂载的内容, 然后其内容就会被渲染到相同 name 的 MountConsumer 组件下。

function Header() {
  return (
    <div>
      <span>Welcome</span>
      <MountConsumer name="header-action" />  
    </div>
  )
}

function Content() {
  function onClick() {
    // do something ...
  }

  return (
    <div>
      <MountProvider name="header-action">
        <Button onClick={onClick}>Content Action</Button>
      </MountProvider>
    </div>
  )
}

function App() {
  return (
    <div>
      <Header />
      <Content />
    </div>
  )
}

更多用法请参考 API 和 DEMO

API

MountProvider

| 属性 | 类型 | 默认值 | 描述 | | --- | --- | --- | --- | | name | string | | 挂载点标识:其 children 会被挂载到同名的 MountConsumer 下 | | visible? | boolean | ((param?: object) => boolean) | true | 控制其内容是否挂载。当前为 Function 时,param 通过 MountConsumer 传递 | | children? | ReactNode | ((param?: object) => ReactNode) | null | ReactNode 或者接收 param 为参数的 Function,并返回 ReactNode |

MountConsumer

| 属性 | 类型 | 默认值 | 描述 | | --- | --- | --- | --- | | name | string | | 挂载点标识:其会挂载渲染同名 MountProvider 提供的 ReactNode | | param? | any | | 会传递给 MountProvider 中 visible 和 children 的参数 | | children? | ((views: ReactNode[]) => ReactNode) | | 接收所有挂载视图,并返回 ReactNode | | fallback? | ReactNode | null | 当没有任何组件挂载时的默认渲染UI |

DEMO

通过 param 传递参数,通过 visible 控制组件是否渲染

function Header() {
  const [value, setValue] = useState(10);

  return (
    <div>
      <MountConsumer 
        name="header-action" 
        param={{ value }}
      />
    </div>
  )
}

function Content() {
  return (
    <div>
      <MountProvider 
        name="header-action"
        visible={(param: any) => param && param.value % 2 === 0}
      >
        {
          (param: any) => {
            if (param?.value !== undefined) {
              return <h1>current value is: {param.value}</h1>
            }

            return <h1>Nothing</h1>
          } 
        }
      </MountProvider>
    </div>
  )
}

设计灵感

设计灵感来自于 DevExtreme Reactive 中的 TemplateTemplatePlaceholder 的使用方式。

关于 “简洁架构之组件的分离式挂载” 的内容,可以翻阅这篇文章。

createPortal 的区别

我们不必在需要知道目标的 DOM ,而是可以通过指定一致的 “name” 即可将 MountProvider 下的结构提供给 MountConsumer 渲染。

在应用中实现模块UI的解耦和注入方面有着非常好的效果。