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 许可协议。转载请注明出处!
相关文章
使用国内NPM镜像源提高速度
NPM介绍 npm(全称 Node Package Manager,即“node包管理器”)是Node.js默认的、以JavaScript编写的软件包管理系统。 npm可以管理本地项目的所需模块并自动维护依赖情况,也可以管理全局安装的JavaScript工具。 但是由于npm的源在国外,所以国内使用起来动辄10kb/s,甚至根本下载不了,所以使用国内镜像源替代官方源就很有必要了。 国内源 淘宝np
2022-02-25 · Web前端 · 约 1 分钟
配置Chrome支持网页内的frame跨域
前言 跨域限制可以保证安全,但是调试的时候关掉会更方便,然而现在网络上能找到的关闭跨域限制方法,在新版的Chrome浏览器上根本没用…… 经过一番摸索,发现用旧版的Chrome就可以绕过跨域限制,刚好我做的这个需要跨域内嵌frame的网页只是单纯展示的,所以这个正好符合我的需求,奥利给! 然后我找到了旧版的Chrome软件,解压出来是绿色版的可以直接运行,不过运行后发现直接把系统的Chrome给调
2021-12-18 · Web前端 · 约 1 分钟
纯前端实现词云展示+附微博热搜词云Demo代码
前言 最近工作中做了几个数据可视化大屏项目,其中也有用到了词云展示,以前做词云都是用python库来生成图片显示的,这次用了纯前端的实现~~(Ctrl+V真好用)~~,同时顺手做个微博热搜的词云然后记录一下~ 依赖 echarts 4.x echarts-wordcloud 1.1.3 tips:echarts-wordcloud现在有2.0和1.x两个版本,2.0对应echarts 5.x版本
2021-12-18 · Web前端 · 约 1 分钟
代码使我头疼之React初学习
前言 开始了,去年(2020)说要学的React,到现在2021年的12月底了,才来实施……(年底警告!~~年末总结还没开始写!~~) 不过前端为啥要学React呢?Vue不是很好用吗?Vue确实很好用,并且MVVM模式也更容易让人接受和理解,但大概是各种社区上都说react yyds啦,react一统江山啦什么的,加上我之前学Flutter做了很多项目,对声明式UI的思想已经基本掌握了,所以就想
2021-12-26 · Web前端 · 约 1 分钟
讨论
共 0 条加载评论中…