4

I'm new to React and I would like to develop a Single Page Application, so I'm using react-router four routing.

Below the main.js, where I specify the routes

import React from 'react';
import {Router,Route} from 'react-router';
import {App} from './components/App';
import {Login} from './components/Login';
import {Home} from './components/Home';
import { history } from 'react-router';

React.render(
<Router history={history}>
    <Route path="/" component={App}>
        <Route path="home" component={Home}/>
        <Route path="login" component={Login}/>

    </Route>
</Router>,
document.getElementById('main')
);

And then the App.js, as you can see I want to have a fixed Header and Footer, and then to have the content of the page change dinamically depending on the route.

import React from 'react';
import {Header} from './Header';
import {Footer} from './Footer';

export class App extends React.Component {


render() {
    console.log(this.props.children);
    return (<div>
        <Header/>
        <div className="page-content">
            {this.props.children}
        </div>
        <Footer/>
    </div>);
}


}

With this code, once the application is loaded with the path ("/"), I need to click on the link Home to display the home content, but I would like to be displayed by default, once the application is first loaded.

How can I achieve that?

Thank you!!

fgonzalez
  • 3,787
  • 7
  • 45
  • 79

4 Answers4

5

I think you probably want to use an IndexRoute as described here in the React Router documentation.

Your router would then look something like this:

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRoute component={Home}/>
        <Route path="login" component={Login}/>
    </Route>
</Router>
Haegin
  • 530
  • 3
  • 11
0

It's usefull when nested IndexRoute.

<div>
    <Redirect from="/" to="home"/>
    <Route path="/" component={App}>
        <Route path="home" component={Home}>
            <IndexRoute component={IndexView} />
            <Route path="other" component={OtherView}></Route>
        </Route>
        <Route path="about" component={About}></Route>

    </Route>
</div>
xsong
  • 630
  • 1
  • 5
  • 18
0

In later versions of react-router, you can use ÌndexRedirect. This way you're not obligated to put your home screen under the route "/". Users navigating to "/" will simply be redirected to "/home".

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRedirect to="home"/>
        <Route path="home" component={Home}/>
        <Route path="login" component={Login}/>
    </Route>
</Router>
Anthony De Smet
  • 2,265
  • 3
  • 17
  • 24
0

Things have changed a lot in React v6. I think you can now achieve what you want with the following specification:

  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} >
        <Route path="/home" element={<Home/>} />
        <Route path="/login" element={<Login />} />
        <Route path="/" element={<Home />} />
      </Route>
    </Routes>
  </BrowserRouter>

If the specification of the App component declares it as an Outlet, the Routes code above will always render it, irrespective of whether the path is "/", "/home" or "/login". Within the nested group, "/home" or "/login" paths will then add Home or Login displays as you would expect, but a "/" path will also add Home as a "default".

Alternatively, but perhaps less comprehensibly, you can use the "index" keyword to render a default Home component within the nested group:

       <Route index element={<Home />} />
MartinJ
  • 333
  • 2
  • 13