{"id":7894,"date":"2015-11-06T16:35:40","date_gmt":"2015-11-06T16:35:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/06\/react-router-urls-dont-work-when-refreshing-or-writting-manually-open-source-projects-rackt-react-router\/"},"modified":"2015-11-06T16:35:40","modified_gmt":"2015-11-06T16:35:40","slug":"react-router-urls-dont-work-when-refreshing-or-writting-manually-open-source-projects-rackt-react-router","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/06\/react-router-urls-dont-work-when-refreshing-or-writting-manually-open-source-projects-rackt-react-router\/","title":{"rendered":"React-router urls don&#39;t work when refreshing or writting manually-open source projects rackt\/react-router"},"content":{"rendered":"<p>The router can be called in two different ways, depending on whether the navigation occurs on the client or on the server. You have it configured for client-side operation. The key parameter is the second one to the run method, the location.<\/p>\n<p>When you use the React Router Link component, it blocks browser navigation and calls transitionTo to do a client-side navigation. You are using HistoryLocation, so it uses the HTML5 history API to complete the illusion of navigation by simulating the new URL in the address bar. If you&#8217;re using older browsers, this won&#8217;t work. You would need to use the HashLocation component.<\/p>\n<p>When you hit refresh, you bypass all of the React and React Router code. The server gets the request for <code>\/joblist<\/code> and it must return something. On the server you need to pass the path that was requested to the <code>run<\/code> method in order for it to render the correct view. You can use the same route map, but you&#8217;ll probably need a different call to <code>Router.run<\/code>. As Charles points out, you can use URL rewriting to handle this. Another option is to use a node.js server to handle all requests and pass the path value as the location argument.<\/p>\n<p>In express, for example, it might look like this:<\/p>\n<pre><code>var app = express();\n\napp.get('*', function (req, res) { \/\/ This wildcard method handles all requests\n\n    Router.run(routes, req.path, function (Handler, state) {\n        var element = React.createElement(Handler);\n        var html = React.renderToString(element);\n        res.render('main', { content: html });\n    });\n});\n<\/code><\/pre>\n<p>Note that the request path is being passed to <code>run<\/code>. To do this, you&#8217;ll need to have a server-side view engine that you can pass the rendered HTML to. There are a number of other considerations using <code>renderToString<\/code> and in running React on the server. Once the page is rendered on the server, when your app loads in the client, it will render again, updating the server-side rendered HTML as needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The router can be called in two different ways, depending on whether the navigation occurs on the client or on the server. You have it configured for client-side operation. The key parameter is the second one to the run method, the location. When you use the React Router Link component, it blocks browser navigation and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7894","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7894"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7894\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}