简体   繁体   English

如何使用 React-Router 默认显示路由

[英]How to display a Route by default with React-Router

I'm new to React and I would like to develop a Single Page Application, so I'm using react-router four routing.我是 React 新手,我想开发一个单页应用程序,所以我使用 react-router 四路由。

Below the main.js, where I specify the routes在 main.js 下方,我在其中指定路线

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.然后是 App.js,如您所见,我希望有一个固定的页眉和页脚,然后让页面的内容根据路线动态变化。

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.使用此代码,一旦使用路径(“/”)加载应用程序,我需要单击链接 Home 以显示主页内容,但我希望在首次加载应用程序后默认显示。

How can I achieve that?我怎样才能做到这一点?

Thank you!!谢谢!!

I think you probably want to use an IndexRoute as described here in the React Router documentation.我认为您可能想要使用 React Router 文档中描述IndexRoute

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>

It's usefull when nested IndexRoute.它在嵌套 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>

In later versions of react-router , you can use ÌndexRedirect .在更高版本的react-router中,您可以使用ÌndexRedirect This way you're not obligated to put your home screen under the route "/".这样您就没有义务将主屏幕放在“/”路径下。 Users navigating to "/" will simply be redirected to "/home".导航到“/”的用户将被简单地重定向到“/home”。

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRedirect to="home"/>
        <Route path="home" component={Home}/>
        <Route path="login" component={Login}/>
    </Route>
</Router>

Things have changed a lot in React v6.在 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".如果 App 组件的规范将其声明为 Outlet,则上面的 Routes 代码将始终呈现它,而不管路径是“/”、“/home”还是“/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".在嵌套组中,“/home”或“/login”路径将按照您的预期添加 Home 或 Login 显示,但“/”路径也会将 Home 添加为“默认”。

Alternatively, but perhaps less comprehensibly, you can use the "index" keyword to render a default Home component within the nested group:或者,但可能不太容易理解,您可以使用“index”关键字在嵌套组中呈现默认 Home 组件:

       <Route index element={<Home />} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM