react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Routing outside react component

Open marcelobateira opened this issue 11 years ago • 1 comments

Hello everyone, I would like to know if this is possible to be done:

  1. Outside the React components I have an header with two links
  2. Those links will redirect to Page1\Index.cshtml or PageTwo/Index.cshtml
  3. Those Index.cshtml have a layout and nothing to render for now...an app.js is loaded that do the rendering of those pages
  4. The app has this:

var $ = require('jquery'); require('materialize'); var React = require('react'); var ReactRouter = require('react-router'); var Router = ReactRouter.Router var Route = ReactRouter.Route var routes = require('./routes.jsx');

React.render(( routes ), document.querySelector('.view-area'));

in my routes.jsx I have:

var Page1 = require('./components/Page1.jsx'); var Page2 = require('./components/Page2.jsx'); var routes = (

<Router >
        <Route path="Page1" component={Page1} />
        <Route path="Page2" component={Page2} />
         <Route path="/" component={Page1}/> //I don't have a main component so by default I put this 
    </Router>

);

What is happening is: When I click on those two links, it is always using the route with the path "/" and renders the Page1

My question is: Can I have react routing that redirects correctly without having a main route/main component? I'm just using react components at a certain point in my app, so those two links are not react components, they just come from a normal cshtml page.

Thanx in advance

marcelobateira avatar Sep 21 '15 09:09 marcelobateira

After some research I answered this question myself: I Implemented the library history: var history = require('history') var createHistory = history.createHistory();

in my router:

<Router  history={createHistory}>

Works great ;)

marcelobateira avatar Sep 22 '15 08:09 marcelobateira