简体   繁体   English

在现有应用程序中将React应用程序与React-Router 4嵌入

[英]Embedding React app with React-Router 4 in Existing App

I have an existing rails app that has a lot of core functionality and I've built out a react router app that will exist on a particular wildcard path of that rails app's routing. 我有一个现有的具有许多核心功能的Rails应用程序,并且已经构建了一个将在该Rails应用程序路由的特定通配符路径中存在的react路由器应用程序。

How do I stop the react-routes plugin from intercepting all links 如何阻止React-Routes插件拦截所有链接

Say I have mounted my app at /reactApp/* and I have other links like /dashboard, /account etc, the react-routes plugin will intercept the external link clicks (the react app is mounted on a div in the middle of an existing rails layout). 假设我已经在/ reactApp / *上安装了我的应用程序,并且还有其他链接,例如/ dashboard,/ account等,则react-routes插件将拦截外部链接点击(react应用程序安装在现有现有中间的div上导轨布局)。 I'd like it to only intercept the link clicks that are defined as routes in the actual react application. 我希望它仅拦截实际React应用程序中定义为路线的链接点击。

I've put together a basic proof of concept here as to how to do this, it's actually quite cool. 关于如何执行此操作,我在这里整理了基本的概念证明,它实际上很酷。 For all of the routes that you want to have on your react app, say it's 'superwidget/*' do the following 对于您希望在React应用程序中拥有的所有路线,请说是“ superwidget / *”,请执行以下操作

#routes.rb
 root to: 'dashboard#index'
 get 'superwidget', to: 'react#index'
 get '*superwidget', to: 'react#index'

You'll want to on the 'react#index' component set up skip_before_action :verify_authenticity_token 您需要在“ react#index”组件上设置skip_before_action :verify_authenticity_token

You can then mount your react app and your rails app will take the URLs it can handle. 然后,您可以挂载您的react应用程序,并且rails应用程序将使用它可以处理的URL。 If you intelligently manage the route schemes you should be tickety boo. 如果您聪明地管理路由方案,那么您应该很轻松。

I've even put together an (extremely bad) sample repo here https://github.com/Proximaio/rails-react-example 我什至在这里https://github.com/Proximaio/rails-react-example整理了一个(极其糟糕的)示例仓库

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

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