javascript - 使用来自 url 将 React.js 组件注入到静态 HTML 站点?
问题描述
我想知道是否可以通过引用将外部站点的 React.js 代码添加到静态 HTML 的 Head 中:
<script src="https://somesite.com/code.js"></script>
在里面,使用一个函数来添加 React.js 库,我想要的代码会出现在静态 HTML 网站上:
let reactScript1 = document.createElement('script');
reactScript1.src = 'https://unpkg.com/react@16/umd/react.development.js';
document.head.append(reactScript1)
let reactScript2= document.createElement('script');
reactScript2.src = 'https://unpkg.com/react-dom@16/umd/react-dom.development.js';
document.head.append(reactScript2)
let reactScript3 = document.createElement('script');
reactScript3.src = 'https://unpkg.com/babel-standalone@6.15.0/babel.min.js';
document.head.append(reactScript3)
ReactDOM.render(
<React.StrictMode>
<Alert/>
</React.StrictMode>,
document.getElementById('root')
);
function Alert () {
const [count, setCount] = React.useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
)
}
我敢肯定这是可能的,但如何?
谢谢!
解决方案
推荐阅读
- c - 使用范围外声明
- python-3.x - HDFStore 初始化错误:名称未定义
- css - RWD 和智能手机的屏幕 x 浏览器窗口大小
- python - Python 字典迭代:d 与 d.viewkeys()
- r - check.length("fill") 中的错误:'gpar' 元素 'fill' 的长度不得为 0
- php - Google Drive API 上传/下载 - PHP 到客户端
- autoencoder - keras 中的成本函数
- c++ - C++ 节点分配错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)
- python - 无法在 OS X 上将 MySQL 导入 python
- laravel - Laravel 架构列