不用脚手架工具,可以在html中使用react和jsx吗?
当然是可以的,要在html中使用react和jsx需要引入react、react-dom、babel三个库,babel可以解析jsx
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>如何在html中使用react和jsx</title>
</head>
<body>
<div id="root"></div>
<script src="https://cdn.bootcdn.net/ajax/libs/react/18.3.1/umd/react.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react-dom/18.3.1/umd/react-dom.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.24.10/babel.min.js"></script>
<script type="text/babel">
function App() {
const [name, setName] = React.useState("志博")
return (
<div>
<div>hello, {name}</div>
<input value={name} onChange={e => setName(e.target.value)} />
</div>
)
}
const container = document.getElementById('root')
const root = ReactDOM.createRoot(container)
root.render(<App/>)
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>如何在html中使用react和jsx</title>
</head>
<body>
<div id="root"></div>
<script src="https://cdn.bootcdn.net/ajax/libs/react/18.3.1/umd/react.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react-dom/18.3.1/umd/react-dom.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.24.10/babel.min.js"></script>
<script type="text/babel">
function App() {
const [name, setName] = React.useState("志博")
return (
<div>
<div>hello, {name}</div>
<input value={name} onChange={e => setName(e.target.value)} />
</div>
)
}
const container = document.getElementById('root')
const root = ReactDOM.createRoot(container)
root.render(<App/>)
</script>
</body>
</html>