REACT中的404页

Liz*_*ody 7 javascript http-status-code-404 reactjs

我创建了该组件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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

如您所见,我曾经<Route path="*" component={NotFound} />用来创建404页面,但是该组件也出现在每个现有页面中。我怎样才能解决这个问题?

sul*_*tan 10

试试这个:

  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>
Run Code Online (Sandbox Code Playgroud)


Ped*_*ram 7

以下所有示例都可以正常工作:

<Route path="" component={NotFound} /> // empty ""
<Route path="*" component={NotFound} /> // star *
<Route component={NotFound} /> // without path
Run Code Online (Sandbox Code Playgroud)

或者,如果您想返回一个没有任何组件的简单 404 消息:

<Route component={() => (<div>404 Not found </div>)} />
Run Code Online (Sandbox Code Playgroud)


Ant*_*ños 6

对于那些使用 v6 寻找答案的人来说react-router-dom,很多事情都发生了变化。Switch例如不再存在,你必须使用element, component... 检查这个小例子让你有一个想法:

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'

import './index.css'
import App from './App'

const Test = () => (
  <h1>404</h1>
)

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <Routes>
        <Route path='/' element={<App />} />
        <Route path='*' element={<Test />}/>
      </Routes>
    </Router>
  </React.StrictMode>,
  document.getElementById('root')
)
Run Code Online (Sandbox Code Playgroud)

这样您就定义了您的home路线,所有其他路线将显示 404。请查看官方指南以获取更多信息。