Skip to content

Using react router

Seoung Ho Jeong edited this page Apr 14, 2017 · 4 revisions

How to use react router in react-rails?

solution of origamih from this issue

  1. Download the React-router library https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.5/ReactRouter.min.js
    Put it in vendor/assets/javascripts, require it in application.js
    Or you can put it in app/assets/javascript (no need to require)
  2. Define the essential React-router variables some where in those javascript asset files.
varRouter=ReactRouter.Router;varRoute=ReactRouter.Route;varLink=ReactRouter.Link;varIndexRoute=ReactRouter.IndexRoute;varIndexLink=ReactRouter.IndexLink;varIndexRedirect=ReactRouter.IndexRedirect;varhashHistory=ReactRouter.hashHistory;varbrowserHistory=ReactRouter.browserHistory;
  1. Add the react_component helper into your view, says the component is ReactHome, this will render the Router component
<%= react_component 'ReactHome'%>
  1. Add the jsx code for Router, in this code you can add the actual React component
varReactHome=React.createClass({render(){return(<Routerhistory={hashHistory}><Routepath="/"component={your_react_component}></Route></Router>);}});

Clone this wiki locally