You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I'm trying to use React-Router in my rails project with React-Rails and it seems to not want to correctly display the component I want when I'm on a specific route.
In the above, I'm only using the setTimeout because it seems to not be able to find anything on page load and says that there is no id main inside the document. setTimeout fixes this.
The App component is just an empty component because I really do not care about it one bit. I'm only rendering a react component when I'm on the /messages route (which on my index.html.haml I am doing the = react_component 'MessageSystem in the corresponding rails view.
I really only want the /messages to display the MessageSystem. That means when I'm on localhost:3000/messages it adds the history hash after the /messages so now it looks like localhost:3000/messages#/?_k=vaeumq.
When I use the React Dev Tools, I see my MessageSystem component when on the page, and then under it I see the Router and the path seems to be just "/". What I think it should be is path="/messages".
Was wondering if anyone else has had any issues using React-Router with react-rails? Thanks
Sadly since this is inside rails, there's no App component. I have specific features that have the mother "system" component, but no App component that encompasses everything if that's what you mean?
I'm a complete newbie in this as well, but if you can humor me with this attempt. Try setting the root path as DefaultRoute, then create an App component that returns only
I've made React-router work with this gem. Here is my solution:
Download the React-router library https://unpkg.com/react-router/umd/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)
Define the essential React-router variables some where in those javascript asset files.
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var Link = ReactRouter.Link;
var IndexRoute = ReactRouter.IndexRoute;
var IndexLink = ReactRouter.IndexLink;
var IndexRedirect = ReactRouter.IndexRedirect;
Add the react_component helper into your view, says the component is ReactHome, this will render the Router component <%= react_component 'ReactHome' %>
Add the jsx code for Router, in this code you can add the actual React component
Good luck. The only problem I got is when the page renders a React component, react-rails-hot-loader doesn't work within that component, it works with child components
Hi, Thanks for sharing the solution, I got this working on my existing rails app. I cant seem to get it to work on pre-rendering though, it keeps giving me the error:
#<ExecJS::ProgramError: TypeError: Cannot read property 'getCurrentLocation' of undefined>
Google shows this to be that it cant find the history for the Router, which are defined in the js assets:
var hashHistory = ReactRouter.hashHistory;
var browserHistory = ReactRouter.browserHistory;
I tried putting these in the server_rendering.js along with react router include, but it still throws the same error. I attempted to put the components after the declaration of the vars, but then the server renderer couldnt find my components... I cant seem to figure out how to debug ExecJS, but it seems to me there is some kind of error with the definitions causing the whole process to fail...
For react-router-dom 4.x
You should change the var, for the new equivalents
var Router = ReactRouterDOM.BrowserRouter;
var Route = ReactRouterDOM.Route;
var Link = ReactRouterDOM.Link;
var HashRouter = ReactRouterDOM.HashRouter;
var Switch = ReactRouterDOM.Switch;
var StaticRouter = ReactRouterDOM.StaticRouter;
var Redirect = ReactRouterDOM.Redirect;
var Prompt = ReactRouterDOM.Prompt;
var NavLink = ReactRouterDOM.NavLink;
var withRouter = ReactRouterDOM.withRouter;
var match = ReactRouterDOM.matchPath;
(I'm guesing this is not the complete list, but you get the idea)
I know this is old issue, but i am trying to wrap my head around similar issue, I am struggling to connect wires in rails router to forward requests to frontend router, and to make it not look like whole page is loading.
I tried a solution to create a controller action, have it render main App.js component and put a line in routes.rb to let it know that all requests except some would go to this action to render front-end app. but this still makes server request and makes react router kind of useless because whole page loads.
Activity
murtali commentedon Mar 3, 2016
having trouble rendering the routers from rails as well. how were you able to render via render_component?
nguyenmv2 commentedon Apr 5, 2016
Do you have in your App component ? I do believe that's where the components of the route are generated
jamby commentedon Apr 5, 2016
Sadly since this is inside rails, there's no App component. I have specific features that have the mother "system" component, but no App component that encompasses everything if that's what you mean?
nguyenmv2 commentedon Apr 5, 2016
I'm a complete newbie in this as well, but if you can humor me with this attempt. Try setting the root path as DefaultRoute, then create an App component that returns only
huy-thp commentedon Oct 14, 2016
I've made React-router work with this gem. Here is my solution:
Put it in vendor/assets/javascripts, require it in application.js
Or you can put it in app/assets/javascript (no need to require)
<%= react_component 'ReactHome' %>Good luck. The only problem I got is when the page renders a React component, react-rails-hot-loader doesn't work within that component, it works with child components
rmosolgo commentedon Dec 9, 2016
Thanks for sharing your solution! I hope it works for others, too!
Kario commentedon May 10, 2017
Hi, Thanks for sharing the solution, I got this working on my existing rails app. I cant seem to get it to work on pre-rendering though, it keeps giving me the error:
#<ExecJS::ProgramError: TypeError: Cannot read property 'getCurrentLocation' of undefined>
Google shows this to be that it cant find the history for the Router, which are defined in the js assets:
var hashHistory = ReactRouter.hashHistory;
var browserHistory = ReactRouter.browserHistory;
I tried putting these in the server_rendering.js along with react router include, but it still throws the same error. I attempted to put the components after the declaration of the vars, but then the server renderer couldnt find my components... I cant seem to figure out how to debug ExecJS, but it seems to me there is some kind of error with the definitions causing the whole process to fail...
Has anyone gotten router to work on pre-render?
Sporky023 commentedon May 19, 2017
Thanks @origamih.
Latest version of react-router on unpkg has a new url format: https://unpkg.com/react-router@4.1.1/umd/react-router.js
eveevans commentedon Aug 30, 2017
For react-router-dom 4.x
You should change the var, for the new equivalents
(I'm guesing this is not the complete list, but you get the idea)
Kastrcn commentedon Nov 7, 2018
how to ssr?
ziaulrehman40 commentedon Nov 10, 2019
I know this is old issue, but i am trying to wrap my head around similar issue, I am struggling to connect wires in rails router to forward requests to frontend router, and to make it not look like whole page is loading.
I tried a solution to create a controller action, have it render main
App.jscomponent and put a line inroutes.rbto let it know that all requests except some would go to this action to render front-end app. but this still makes server request and makes react router kind of useless because whole page loads.