我想知道是否可以通过引用将外部站点的 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>
)
}
我敢肯定这是可能的,但如何?
谢谢!