简体   繁体   English

我可以将 React 应用程序用作静态 HTML 页面上的组件吗

[英]Can I use a React app as a component on a static HTML page

I'm new to React and JS, and working with react-csv-viewer .我是 React 和 JS 的新手,正在使用react-csv-viewer

It works as expected and I can build and deploy it on a local server.它按预期工作,我可以在本地服务器上构建和部署它。 But I do not require this, I just want to integrate the app as a component of a static HTML page.但我不需要这个,我只想将应用程序集成为静态 HTML 页面的组件。

I've tried following the process listed on the React tutorial for this, but I have trouble understanding the build process and how can I achieve this.我已经尝试按照React 教程中列出的过程进行操作,但是我无法理解构建过程以及如何实现这一点。

All I wish to achieve is to be able to use <CsvViewer /> provided by the author, possibly like this我希望实现的只是能够使用作者提供的<CsvViewer /> ,可能像这样

const rootElement = document.getElementById("root");
ReactDOM.render(<CsvViewer />, rootElement);

and get the viewer app rendered at my HTML file, without building and deploying the viewer app on a (local) server.并在我的 HTML 文件中呈现查看器应用程序,而无需在(本地)服务器上构建和部署查看器应用程序。

Will appreciate any help or hints in this regard.将不胜感激这方面的任何帮助或提示。

You can add React as it was shown in the tutorial you linked.您可以添加 React,如您链接的教程中所示。 The downside is that, you can't use JSX syntax (as it should be converted to JS during build time as it's not recognized by browsers as so).缺点是,您不能使用 JSX 语法(因为它应该在构建时转换为 JS,因为浏览器无法识别它)。

Here is a post explaining how you can do so without transpilaton steps.这是一篇解释如何在没有 transpilaton 步骤的情况下做到这一点的帖子。

https://codepen.io/alexkrolick/post/react-without-a-build-step https://codepen.io/alexkrolick/post/react-without-a-build-step

Alias React.createElement , and call it to create components.别名React.createElement ,并调用它来创建组件。 eg)例如)

const h = createElement // convenient alias

// Instead of
<div className="foo" />

// create an element like so
h("div", { className: "foo" })

For more info on how that works, check out the official documentation.有关其工作原理的更多信息,请查看官方文档。
https://reactjs.org/docs/introducing-jsx.html#jsx-represents-objects https://reactjs.org/docs/introducing-jsx.html#jsx-represents-objects

But I really doubt anyone writes React code that way without a transpilation step in real life.但我真的怀疑有人会在现实生活中没有转换步骤的情况下以这种方式编写 React 代码。

Would creating a separate site/page with React be a problem by chance?使用 React 创建一个单独的站点/页面会是一个偶然的问题吗? You can check out ParcelJS to easily create a React site if you aren't familiar with transpilation.如果您不熟悉转译,您可以查看ParcelJS以轻松创建 React 站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM