How to use React Router on top of Rails router (not using react-rails gem)?

后端 未结 2 1304
礼貌的吻别
礼貌的吻别 2020-12-16 11:24

Now i am making React app on top off Ruby on Rails app (with out react-rails gem) by using browserify-rails to compile js files.

So i tried to us react-router to con

相关标签:
2条回答
  • 2020-12-16 12:13

    You can also specify all the URLs you need in Router, manually in routes.rb, redirecting them to your rails controller with the Router, say /home:

    # config/routes.rb 
    Rails.application.routes.draw do
      root action: :index, controller: 'home'
    
      get 'url1', action: :index, controller: 'home'
      get 'url2', action: :index, controller: 'home'
      get 'url3', action: :index, controller: 'home'
    end
    

    Note that your base React component will use Router to use the corresponding component, if you have something like:

    <Route path="/url1" component={ ComponentForUrl1 } />
    <Route path="/url2" component={ ComponentForUrl3 } />
    <Route path="/url3" component={ ComponentForUrl2 } />
    
    0 讨论(0)
  • 2020-12-16 12:19

    If you want to redirect all your request to single page, that's easy:

    # config/routes.rb 
    
    root 'dash_board#index'
    get '*path', to: 'dash_board#index'
    

    If Rails will render your React components on DashBoard#index page, React's router will intercept it from there.

    0 讨论(0)
提交回复
热议问题