
Company News
Socket Partners with Replit to Block Malicious Packages in AI-Powered Development
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.
@empjs/adapter-react
Advanced tools
一个轻量级的 React 适配器,用于 EMP 微前端框架中跨 React 版本渲染组件。
npm install @empjs/adapter-react
import { reactAdapter } from '@empjs/adapter-react'
import MyComponent from './MyComponent'
// 适配一个组件
const WrappedComponent = reactAdapter.adapter(MyComponent)
// 使用适配后的组件
<WrappedComponent {...props} />
import { ReactAdapter } from '@empjs/adapter-react'
import React from 'react'
import ReactDOM from 'react-dom'
// 创建自定义适配器实例
const adapter = new ReactAdapter({
React,
ReactDOM,
createRoot: ReactDOM.createRoot, // React 18+ 需要
scope: 'default'
})
// 适配组件
const WrappedComponent = adapter.adapter(MyComponent)
import { ReactAdapter } from '@empjs/adapter-react'
// 从全局对象获取 React18 的对象
const { EMP_ADAPTER_REACT } = window as any
const react18 = new ReactAdapter(EMP_ADAPTER_REACT)
const RemoteApp = react18.adapter(remoteComponent)
import { reactAdapter } from '@empjs/adapter-react'
// 适配动态导入的组件
const AsyncComponent = import('./RemoteComponent')
const WrappedAsyncComponent = reactAdapter.adapter(AsyncComponent, 'default')
// 在 JSX 中使用
<WrappedAsyncComponent {...props} />
创建一个新的适配器实例。
参数:
options (可选): ReactAdapterOptions - 适配器配置选项
React?: 自定义的 React 库实例ReactDOM?: 自定义的 ReactDOM 库实例createRoot?: React 18+ 的 createRoot 方法scope?: 组件导出的作用域名称,默认为 'default'示例:
// 使用默认配置
const adapter = new ReactAdapter()
// 使用自定义配置
const adapter = new ReactAdapter({
React: CustomReact,
ReactDOM: CustomReactDOM,
createRoot: CustomReactDOM.createRoot
})
适配一个 React 组件,返回包装后的组件。
参数:
component: 要适配的 React 组件或异步组件scope? (可选): 组件的作用域名称,默认使用实例配置的 scopeReact? (可选): 要使用的 React 库,默认使用实例配置的 ReactReactDOM? (可选): 要使用的 ReactDOM 库,默认使用实例配置的 ReactDOM返回值:
示例:
// 基本用法
const WrappedComponent = reactAdapter.adapter(MyComponent)
// 指定作用域
const WrappedWithScope = reactAdapter.adapter(MyComponent, 'customScope')
// 使用自定义 React 和 ReactDOM
const CustomWrapped = reactAdapter.adapter(
MyComponent,
'default',
CustomReact,
CustomReactDOM
)
ReactAdapter 通过以下机制实现跨 React 版本的组件渲染:
ReactDOM.render 和 ReactDOM.unmountComponentAtNodecreateRoot 创建根节点并调用 render 和 unmount 方法// 主应用
import { ReactAdapter } from '@empjs/adapter-react'
import remoteApp from 'remote/App'
import React from 'react'
// 从全局配置初始化适配器
const { EMP_ADAPTER_REACT } = window as any
const react18 = new ReactAdapter(EMP_ADAPTER_REACT)
// 适配远程应用组件
const RemoteApp = react18.adapter(remoteApp)
// 在 React 16 环境中使用 React 18 组件
const App = () => (
<div>
<h1>主应用 (React 16)</h1>
<RemoteApp>
<div>这是来自主应用的子内容</div>
</RemoteApp>
</div>
)
export default App
createRoot 方法scope 参数指定导出的组件名称FAQs
emp react adapter
The npm package @empjs/adapter-react receives a total of 10 weekly downloads. As such, @empjs/adapter-react popularity was classified as not popular.
We found that @empjs/adapter-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.

Research
/Security News
Newer packages in this compromise use native extensions and .pth loaders to execute JavaScript stealers in developer environments.