有没有办法在运行时从外部源加载文件内容?
const MyComponent = (props) => {
const [dynamicComponent, fetchComponent] = React.useState(null);
const loadComponentDynamically = ()=> {
const _dynamicComponent = (/*import component from external source <external url>/somecomponent.js*/);
fetchComponent(_dynamicComponent );
};
return (
<div>
<button onClick={loadComponentDynamically }>Load component</button>
{dynamicComponent && dynamicComponent()}
</div>
);
};