简体   繁体   English

将React Router与react-rails gem一起使用

[英]Using React router with react-rails gem

I am an experienced ruby on rails developer and trying to use reactjs with it. 我是一个有经验的Ruby on Rails开发人员,并试图将reactjs一起使用。 So I installed react-rails gem and followed the instructions. 因此,我安装了react-rails gem并按照说明进行操作。

There are two possible approaches to get started: 有两种可能的入门方法:

  1. Using asset pipeline 使用资产管道
  2. Using webpacker 使用webpacker

Webpacker gem looks pretty new and I had hard time getting the webpacker-dev-server work with my current rails app. Webpacker gem看起来很新,我很难将webpacker-dev-server与当前的Rails应用程序一起使用。 And also running two process using foreman (one for rails and one for webpacker) looks too many dependency. 而且还使用工头运行两个进程(一个用于rails,一个用于webpacker)看起来过于依赖。

So I went ahead with asset pipeline approach by placing all my React components in app/assets/javascripts/components/MyComponent.jsx . 因此,我通过将所有React组件都放在app/assets/javascripts/components/MyComponent.jsx进行资产管道方法。 Everything works fine and I am able to render the component using <%=reach_component("MyComponent")%> 一切正常,我可以使用<%=reach_component("MyComponent")%>渲染组件

Now I want to design a Single Page app around it. 现在,我想围绕它设计一个单页应用程序。 Hence I started exploring about react-router . 因此,我开始探讨react-router But there is no proper explanation on how to use react-router with react-rails gem. 但是,关于如何将react-routerreact-rails gem结合使用,没有适当的解释。

I tried adding the react-router package in packages.json and ran yarn but ended up with the following error on browser after all the include. 我尝试在packages.json添加react-router package packages.json并运行yarn但是在所有包含之后,在浏览器中最终出现以下错误。

Uncaught ReferenceError: exports is not defined

Any help on getting started with react-router and rails without webpack and without using babel in browser and making client heavy ? 在没有webpack且没有在浏览器中使用babel并使客户端沉重的情况下使用React-router和rails有任何帮助吗?

The browser error is because you've got CommonJS code (ie exports statements) that is not being translated into a format suitable for running on the browser. 浏览器错误是因为您没有将CommonJS代码(即export语句)转换为适合在浏览器上运行的格式。

I haven't tried to get React running in rails so am not sure if the asset pipeline is capable of working with node libraries, but you might need to consider switching your approach to something like this: 我还没有尝试过让React在Rails中运行,所以不确定资产管道是否能够与节点库一起工作,但是您可能需要考虑将方法切换为以下方式:

http://blog.arkency.com/2015/03/gulp-modern-approach-to-asset-pipeline-for-rails-developers/ http://blog.arkency.com/2015/03/gulp-modern-approach-to-asset-pipeline-for-rails-developers/

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

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