简体   繁体   English

将 React 与 JSX 的现有应用程序结合使用

[英]Using React with an existing application with JSX

We are planning to switch new technologies like react for my CMS project which is under development for 10 years.我们计划为我开发了 10 年的 CMS 项目转换新技术,例如 react。

Until now everything was simple and plain on the front end.到目前为止,前端的一切都很简单明了。

First include jquery.js then if necessary include the components and third party scripts, then code and dance with the DOM.首先包含 jquery.js,然后在必要时包含组件和第三方脚本,然后编码并与 DOM 共舞。

But now while trying to jump into a higher level of technology and different approach, things can easily get very complicated for me.但是现在在尝试跳入更高水平的技术和不同的方法时,事情对我来说很容易变得非常复杂。

After spending more than 10 hours with React documents and tutorials I have a very good understanding about what it is and how it works.在花了 10 多个小时阅读 React 文档和教程之后,我对它是什么以及它是如何工作的有了很好的理解。

But I realized that I am very unfamiliar with some popular concepts.但是我意识到我对一些流行的概念非常陌生。 I never used node.js, never used npm, babel, webpack, and may other many "new" things I have seen every where.我从来没有用过 node.js,从来没有用过 npm、babel、webpack,还有很多我在任何地方都见过的“新”的东西。 I am face to face with these tools because of React and I am convinced that these are the inevitable for modern front end development.因为 React,我与这些工具面对面,我相信这些是现代前端开发的必然。

Now the question现在的问题

Our CMS runs on PHP and depends on MooTools heavily at the front end.我们的 CMS 在 PHP 上运行,并且在前端严重依赖 MooTools。 Instead of a complete rewrite of a 10 years old CMS I just want to try new technologies partially for some cases.我只是想在某些情况下部分尝试新技术,而不是完全重写 10 年前的 CMS。 Decided to starting with React.决定从 React 开始。

For the case I want to integrate ag-Grid to React also.对于这种情况,我也想将ag-Grid集成到 React。

What I did not understand is that how to bring all these tools together.我不明白的是如何将所有这些工具组合在一起。

I won't be able to use the simply include js way of react because of ag-Grid.由于 ag-Grid,我将无法使用简单的包含 js 的反应方式。

In the examples the code written has some JSX.在示例中编写的代码有一些 JSX。 Which means that we write JSX and run it translated for the browser to test if it is ok.这意味着我们编写 JSX 并运行它为浏览器翻译以测试它是否正常。

  • Each time before testing do I need to translate these files?每次测试前我都需要翻译这些文件吗?
  • And more over if the files are translated does debugging become very complicated?如果文件被翻译,调试会变得非常复杂吗?
  • Can babel make it on the run time? babel 可以在运行时完成吗? If yes is it a good practice.如果是,这是一个好习惯。
  • There are lots of file in the node_modules folder. node_modules 文件夹中有很多文件。 Which of them should I include for production?我应该包括哪些用于生产?

All sources on the net are very theoretical and assumes a knowledge.网络上的所有来源都是非常理论化的,并假设有知识。 Need some guidance for best practices.需要一些最佳实践指导。

There are lots of questions and not a single step by step guide from beginning to production.有很多问题,而且没有一个从开始到生产的分步指南。

JSX is an extension over spec-compliant JavaScript. JSX 是对符合规范的 JavaScript 的扩展。 It is syntactic sugar for React.createElement(...) and is optional in React development.它是React.createElement(...)语法糖,在 React 开发中是可选的。

React can be written in plain ES5: React 可以用普通的 ES5 编写:

React.createElement("div", { foo: "foo" });

Instead of JSX:代替 JSX:

<div foo="foo" />

Or with helper functions like h that achieve the same goal, eg react-hyperscript .或者使用像h这样的辅助函数来实现相同的目标,例如react-hyperscript

The fact that there is PHP backend application doesn't prevent from developing React frontend application with JSX.有 PHP后端应用程序的事实并不能阻止使用 JSX 开发 React前端应用程序。 This may require to configure React project to not use built-in Express web server and build client-side application to custom location, ie existing app's public folder.这可能需要将 React 项目配置为不使用内置 Express Web 服务器并将客户端应用程序构建到自定义位置,即现有应用程序的公共文件夹。 In case create-react-app is used, this may require to eject the project).如果使用create-react-app ,这可能需要弹出项目)。

Each time before testing do I need to translate these files?每次测试前我都需要翻译这些文件吗?

They should be transpiled to plain JavaScript (ES5 if it targets older browsers).它们应该被转换为纯 JavaScript(如果它针对旧浏览器,则为 ES5)。 They can be translated on every change in source files when client-side project runs in watch mode (conventionally npm start ).当客户端项目以监视模式(通常是npm start )运行时,它们可以在源文件的每次更改时进行转换。

And more over if the files are translated does debugging become very complicated?如果文件被翻译,调试会变得非常复杂吗?

This is what source maps are for.这就是源映射的用途。

Can babel make it on the run time? babel 可以在运行时完成吗? If yes is it a good practice.如果是,这是一个好习惯。

It's possible to use Babel at runtime, and this isn't a good practice, even in development environment.可以在运行时使用 Babel,这不是一个好习惯,即使在开发环境中也是如此。

There are lots of file in the node_modules folder. node_modules 文件夹中有很多文件。 Which of them should I include for production?我应该包括哪些用于生产?

The contents of node_modules doesn't matter. node_modules 的内容无关紧要。 Almost all of them are development dependencies that are needed to build client-side app.几乎所有这些都是构建客户端应用程序所需的开发依赖项。 This is the task for a bundler, which is Webpack in create-react-app template.这是打包create-react-app的任务,它是create-react-app模板中的 Webpack。 It builds project dependencies to plain JS in dist folder.它在dist文件夹中构建对普通 JS 的项目依赖项。

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

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