gpt4 book ai didi

javascript - 如何在 React 中显示默认 View ?

转载 作者:行者123 更新时间:2023-12-03 23:02:28 24 4
gpt4 key购买 nike

我有一个包含三个不同路由的 React 站点,我希望它自动显示第一个,称为 Home ,当用户进入网站时。这是我在 App.js 中的代码:

<Router>
<Navigation />
<Switch>
<Route path="/hypstats" exact component={() => <Home />} />
<Route path="/hypstats/auctions" exact component={() => <AuctionViewer />} />
<Route path="/hypstats/bazaar" exact component={() => <BazaarViewer />} />
</Switch>
</Router>
这是 Navigation零件:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import "../App.css"

function Navigation(props) {
return (
<div className="navbar">
<Link className="navlink" to="/hypstats">HypStats</Link>
<Link className="navlink" to="/hypstats/auctions">Auctions</Link>
<Link className="navlink" to="/hypstats/bazaar">Bazaar</Link>
</div>
)
}

export default Navigation;

最佳答案

要将任何 View 设为默认 View ,您可以在导航中包含以下内容

<Route path="/" exact component={() => <Home />} />
或者你可以这样写:
<Redirect from="/" to="/hypstats"}  />


<Router>
<Navigation />
<Switch>
<Route path="/hypstats" exact component={() => <Home />} />
<Route path="/hypstats/auctions" exact component={() => <AuctionViewer />} />
<Route path="/hypstats/bazaar" exact component={() => <BazaarViewer />} />
**<Route path="/" exact component={() => <Home />} />**
</Switch>
</Router>

关于javascript - 如何在 React 中显示默认 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64837300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com