Introducing React and JSX in HTML
Vue can be easily integrated with Pure HTML to replace the functionality of jQuery. Once, when encountering a similar scenario, I wondered if React could also be incorporated into HTML web pages in this way to improve development efficiency. Of course, it is possible, but directly using JSX in HTML might slightly reduce performance… just go with it.
目录4 个章节
Preface
Vue can be very conveniently integrated with pure HTML, replacing the functionality of jQuery. When I encountered a similar scenario once, I thought about whether React could also be incorporated into an HTML webpage in this way to improve development efficiency.
The answer is, of course, yes. However, using JSX directly in HTML might lead to some performance degradation... but it works well enough for most cases.
Importing Dependencies
To use React and JSX in Pure HTML, the following dependencies are required:
- react
- react-dom
- babel
You can simply use the unpkg CDN.
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.20.6/babel.min.js"></script>
Getting Started with Coding
Just like with Vue, you need to create a container in your page.
<div id="root"></div>
For JSX, it's recommended to create a separate file for better IDE hints (IDE提示).
Here, we create a new file named app.jsx.
Import it after your dependencies.
<script type="text/babel" src="js/app.jsx"></script>
Then you can start writing JSX directly.
function App() {
const [name, setName] = React.useState("codelab")
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/>)
Done
The actual usage effect is passable at best.
Of course, it's still better to use CRA to create a React application, then bundle and deploy it; otherwise, the performance of complex pages can be quite problematic.
_EOF_
Introducing React and JSX in HTML
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
返璞归真!使用 Alpine.js 开发交互式 web 应用,抛弃 node_modules 和 webpack 吧!
前端框架层出不穷,项目越做越大,alpine.js 和 htmx 这种库是反其道而行,可以用最简单的方法来开发现代化的 web 应用。
2024-11-21 · Web前端 · 约 1 分钟
使用 swagger-typescript-api 在前端项目里生成请求代码
swagger-typescript-api?文档写得像草稿,还得自己摸索。那些花哨的模块化选项除了报错还能干嘛?老老实实生成单文件,配个客户端封装,省得每次折腾。
2025-11-03 · Web前端 · 约 1 分钟
前端邪修:不用Vite也不用Webpack,把React拖回HTML时代的反工程化实践
反工程化React实践?直接HTML引入React写界面?这操作够“邪修”吧。工具链?HMR?不需要,手动加载依赖,浏览器缓存自己控。简单交互页面何必引入整套工程体系?这取舍,够“高冷”。
2025-12-30 · Web前端 · 约 1 分钟
使用mock.js开发假数据接口
参考资料 Mockjs使用 - https://juejin.cn/post/6953199521031520292 http://wll8.gitee.io/mockjs-examples Mokcjs 文档 Vitepress版 - http://mock.pe666.cn/
2022-12-06 · Web前端 · 约 1 分钟
讨论
共 0 条加载评论中…