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 许可协议。转载请注明出处!
相关文章
使用 swagger-typescript-api 在前端项目里生成请求代码
swagger-typescript-api?文档写得像草稿,还得自己摸索。那些花哨的模块化选项除了报错还能干嘛?老老实实生成单文件,配个客户端封装,省得每次折腾。
2025-11-03 · Web前端 · 约 1 分钟
视频中台解决方案:组织树组件+多路视频直播界面开发
这种国产粗犷风格的项目居然能用?16路视频播放界面倒是简单直接,但现场配置和设备管理流程真是够折腾的。WVP和mpegts.js?B站开源的尿性,细节全靠蒙。
2025-07-04 · Web前端 · 约 1 分钟
配置Chrome支持网页内的frame跨域
前言 跨域限制可以保证安全,但是调试的时候关掉会更方便,然而现在网络上能找到的关闭跨域限制方法,在新版的Chrome浏览器上根本没用…… 经过一番摸索,发现用旧版的Chrome就可以绕过跨域限制,刚好我做的这个需要跨域内嵌frame的网页只是单纯展示的,所以这个正好符合我的需求,奥利给! 然后我找到了旧版的Chrome软件,解压出来是绿色版的可以直接运行,不过运行后发现直接把系统的Chrome给调
2021-12-18 · Web前端 · 约 1 分钟
返璞归真!使用 Alpine.js 开发交互式 web 应用,抛弃 node_modules 和 webpack 吧!
前端框架层出不穷,项目越做越大,alpine.js 和 htmx 这种库是反其道而行,可以用最简单的方法来开发现代化的 web 应用。
2024-11-21 · Web前端 · 约 1 分钟
讨论
共 0 条加载评论中…