gpt4 book ai didi

javascript - REACT 中的 404 页面

转载 作者:数据小太阳 更新时间:2023-10-29 04:34:23 26 4
gpt4 key购买 nike

我创建了组件 NotFound当我转到一个不存在的页面时它工作正常。但是同一个页面出现在我的所有页面中,而不仅仅是不存在的页面。这是组件:

import React from 'react'

const NotFound = () =>
<div>
<h3>404 page not found</h3>
<p>We are sorry but the page you are looking for does not exist.</p>
</div>

export default NotFound

这就是我在主页中使用它的方式:

class MainSite extends Component {
render () {
return (
<div>
{/* Render nav */}
<Route path='/dashboard' component={Nav} />
<Route path='/retrospectives' component={Nav} />
<Route path='/users' component={Nav} />
<Route path='/projects' component={Nav} />

{/* Dashboard page */}
<ProtectedRoute exact path='/dashboard' component={DashboardPage} />

{/* Retrospectives page */}
<ProtectedRoute exact path='/retrospectives' component={RetrospectivesPage} />

{/* Users page */}
<ProtectedRoute exact path='/users' component={UsersPage} />

{/* Projects page */}
<ProtectedRoute exact path='/projects' component={ProjectsPage} />

{/* Retrospective related pages */}
<Route exact path='/retrospectives/:retrospectiveId' component={Retrospective} />
<Route exact path='/join-retrospective' component={JoinRetrospective} />
<ProtectedRoute exact path='/create-retrospective/:retrospectiveId' component={Retrospective} />

{/* OnBoarding pages */}
<ProtectedRoute exact path='/beta-code' component={BetaCodeAccess} />
<Route exact path='/auth-handler' component={AuthHandler} />
<Route exact path='/join-organization' component={JoinOrganization} />
</div>
)
}
}

export default MainSite

如你所见,我使用 <Route path="*" component={NotFound} />创建 404 页面,但该组件也出现在每个现有页面中。我该如何解决这个问题?

最佳答案

试试这个:

  import { Switch, Route } from 'react-router-dom';

<Switch>
<Route path='/dashboard' component={Nav} />
<Route path='/retrospectives' component={Nav} />
<Route path='/users' component={Nav} />
<Route path='/projects' component={Nav} />

<Route path="" component={NotFound} />
</Switch>

关于javascript - REACT 中的 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181678/

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