2

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!!

4

3 回答 3

5

我想你可能想使用React Router 文档IndexRoute中描述

然后,您的路由器将如下所示:

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRoute component={Home}/>
        <Route path="login" component={Login}/>
    </Route>
</Router>
于 2015-12-08T18:55:30.293 回答
0

它在嵌套 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>
于 2015-12-10T08:19:38.573 回答
0

在以后的版本中react-router,您可以使用ÌndexRedirect. 这样您就没有义务将主屏幕放在“/”路径下。导航到“/”的用户将被简单地重定向到“/home”。

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRedirect to="home"/>
        <Route path="home" component={Home}/>
        <Route path="login" component={Login}/>
    </Route>
</Router>
于 2016-03-13T16:57:53.863 回答