返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Web前端

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.

曦远 (DealiAxy)约 5 分钟
目录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 许可协议。转载请注明出处!

相关文章

配置Chrome支持网页内的frame跨域

前言 跨域限制可以保证安全,但是调试的时候关掉会更方便,然而现在网络上能找到的关闭跨域限制方法,在新版的Chrome浏览器上根本没用…… 经过一番摸索,发现用旧版的Chrome就可以绕过跨域限制,刚好我做的这个需要跨域内嵌frame的网页只是单纯展示的,所以这个正好符合我的需求,奥利给! 然后我找到了旧版的Chrome软件,解压出来是绿色版的可以直接运行,不过运行后发现直接把系统的Chrome给调

2021-12-18 · Web前端 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…