简体   繁体   English

React Router v4全局与嵌套路由子项不匹配

[英]React Router v4 global no match to nested route childs

How can I redirect the user to a NoMatch component when I have nested routes in React Router V4? 当我在React Router V4中具有嵌套路由时,如何将用户重定向到NoMatch组件?

Here is my code: 这是我的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin();

import {
    BrowserRouter as Router,
    Route,
    Switch
}
from 'react-router-dom';
import Website from './website/Website';

const Register = ({ match}) => {
    return (
        <div>
            <Route exact path={match.url} render={() => {return <h1>Register</h1>} } />
            <Route path={`${match.url}/detail`} render={()=>{return <h1>Register Detail</h1>}} />
        </div>
    )
}

const App = () => (
    <Router>
        <Switch>
                <Route exact path="/" render={() =>  {return <h1>Home</h1> }} />
                <Route path="/register" component={Register} />
                <Route component={() => {return <h1>Not found!</h1>}} />
        </Switch>
    </Router>
);

ReactDOM.render(
    <App/>, document.getElementById('root'));

As You can see, there is a NoMatch route below Register but I don't want to use the same Route mapping on my child component Register. 如您所见,Register下方有一个NoMatch路由,但我不想在子组件Register上使用相同的Route映射。 In this way, if I go to /register/unregisteredmatch the page just show blank because do not enter in NoMatch Route. 这样,如果我转到/ register / unregisteredmatch,则该页面将显示为空白,因为不输入NoMatch Route。

How can I map a global NoMatch without specify that on my child route? 如何在不指定子路线的情况下映射全局NoMatch? I don't want to pass this responsability to the child components. 我不想将此责任传递给子组件。

Thanks. 谢谢。

I have the same problem with Switch since the following will always render my NoMatch component if I do not go to /a 我对Switch有相同的问题,因为如果我不转到/ a,以下内容将始终呈现我的NoMatch组件

<Router history={history}>
  <Switch>
    <Route path='/a' component={A} />
    <Switch>
      <Route path='/b' component={B} />
      <Route path='/b/:id' component={C} />
    </Switch>
    <Route path="*" component={NoMatch}/>
  </Switch>
</Router>

However, it will work, as expected, if you move your NoMatch inside the nested Switch like this: 但是,如果您按如下方式将NoMatch移入嵌套的Switch中 ,它将可以正常工作:

<Router history={history}>
  <Switch>
    <Route path='/a' component={A} />
    <Switch>
      <Route path='/b' component={B} />
      <Route path='/b/:id' component={C} />
      <Route path="*" component={NoMatch}/>  
    </Switch>
  </Switch>
</Router>

Even if this is a 'solution' to the problem it is not what you want since the second Switch is in a different file like the first Route is as well. 即使这是问题的“解决方案”,也不是您想要的,因为第二个Switch与第一个Route一样在不同的文件中。

So as the application grows and more routes come into play in different files you never know where you need to put the NoMatch route for it to work as expected. 因此,随着应用程序的增长以及更多路由在不同文件中起作用,您永远都不知道需要将NoMatch路由放置在何处以使其按预期工作。

Did you find any other solution to this problem? 您是否找到其他解决此问题的方法?

What you could do is define all possibly allowed routes in your root App. 您可以做的是在根应用程序中定义所有可能允许的路由。 Therefore, adapt your App component using optional pattern as follows: 因此,请使用可选模式修改您的App组件,如下所示:

const App = () => (
    <Router>
        <Switch>
                <Route exact path="/" render={() =>  {return <h1>Home</h1> }} />
                <Route path="/register/(detail)?" exact component={Register} />
                <Route component={() => {return <h1>Not found!</h1>}} />
        </Switch>
    </Router>
);

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

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