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

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


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.


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 } />