
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-asyncmodule
Advanced tools
webpack2+, it's recommended to use import method implementation code spliting, this component can help you simplify your code.
npm install react-asyncmodule --save
import AsyncModule from 'react-asyncmodule';
const Home = AsyncModule({
load: () => import(/* webpackChunkName: "home" */ './home'),
resolveWeak: () => require.resolveWeak('./home'),
chunk: () => 'home',
loading: <Loading />,
error: <ErrorView />
});
const List = AsyncModule({
load: () => import(/* webpackChunkName: "list" */ './list'),
resolveWeak: () => require.resolveWeak('./list'),
chunk: () => 'list',
loading: <Loading />,
error: <ErrorView />
});
通过上面的代码,我们可以发现 loading and error 2个选项是相同的,可以这样简化。
const AsyncComponent = AsyncModule({
loading: <Loading />,
error: <ErrorView />
});
const Home = AsyncComponent({
load: () => import(/* webpackChunkName: "home" */ './home'),
resolveWeak: () => require.resolveWeak('./home'),
chunk: () => 'home'
});
const List = AsyncModule({
load: () => import(/* webpackChunkName: "list" */ './list'),
resolveWeak: () => require.resolveWeak('./list'),
chunk: () => 'list'
});
很好,但是还有一些重复的代码。
const Home = AsyncComponent(import('./home'));
const List = AsyncComponent(import('./list'));
完美,使用这个 babel 插件 babel-plugin-asyncmodule-import。
返回的 AsnycComponent 组件上有以下一些静态属性和方法。
属性,返回当前组件的 chunk 名称,即上面代码中的 chunk 方法的返回值。
预加载方法,返回一个 promise 实例,可在回调中获取到当前组件。a
预加载方法,不同于 preload,该方法是同步返回当前的组件,通常用于 server render。
import { asyncReady } from 'react-asyncmodule';
asyncReady(() => {
// 依赖的 chunk 准备就绪,再进行注水,防止因 chunk 还未加载完成和 server 端渲染不一致
ReactDOM.hydrate();
});
AsyncModule(Options);
函数, 返回一个 Promise 实例. 通常由 webpack 提供的 import 方法生成.
自定义渲染函数,接收两个参数分别是 props 和 Component。
const List = AsyncModule({
load: () => import(/* webpackChunkName: "list" */ './list'),
render: (props, Comp) => <Comp {...props} />
});
返回当前导入组件的 moduleId。由 webpack 的 require.resolveWeak 方法自动生成。
加载时显示的组件。
加载错误时显示的组件。 接受两个参数onRetry和error。
默认 200 ms。 loading 组件延迟显示的时间,单位ms。
当网络速度很快时,用户看不到加载组件。
默认 120000 ms。 加载超时,单位ms。
module加载完执行,返回3个参数
onModuleLoaded(comp, chunkName, isServer)
FAQs
react async load component
The npm package react-asyncmodule receives a total of 3 weekly downloads. As such, react-asyncmodule popularity was classified as not popular.
We found that react-asyncmodule demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.