Skip to content

PortalRenderer 命令式Dom

将声明式的组件转换成命令式组件。

基础用法

tsx
import { Button, portalRenderer } from 'ono-react-element'

const MyComponent = ({ destroy }: { destroy: () => void }) => {
  return (
    <div
      style={{
        top: 0,
        left: 0,
        position: 'fixed',
        width: '100vw',
        height: '100vh',
        background: 'rgba(0,0,0,0.5)',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center'
      }}
    >
      <div
        style={{
          width: 500,
          height: 300,
          background: 'white',
          border: '1px solid #333',
          borderRadius: 4,
          position: 'relative',
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center'
        }}
      >
        <h1>This is my component</h1>
        <Button
          style={{ position: 'absolute', right: 16, bottom: 16 }}
          onClick={destroy}
        >
          Close
        </Button>
      </div>
    </div>
  )
}

function App() {
  return (
    <div style={{ width: '100%', display: 'flex', gap: 10 }}>
      <Button onClick={() => portalRenderer(MyComponent, {}, 'my-component')}>
        Open My Component
      </Button>
    </div>
  )
}

export default App;

API

通用属性参考:通用属性

参数说明类型默认值是否必填
Component传入组件的props必须带有destroy函数属性(props: T) => JSX.Element-
props传入组件的props,除了destroy函数属性Omit<T, "destroy">-
iddom的id,destroy函数会根据传入的id删除对应的domstring-

ComponentProps

参数说明类型默认值是否必填
destroy命令式组件的销毁函数(onDestroy?: () => void) => void-