在react-router-v4中将Switch组件与全局NoMatch组件嵌套在一起

dat*_*oml 4 reactjs react-router react-router-v4

我的应用目前分为3部分:

  • 前端
  • 管理
  • 错误

前端,管理和错误组件具有各自的样式。

前端和管理组件还具有其自己的Switch组件以在它们之间进行导航。

我面临的问题是,如果没有重定向组件,我将无法进入NoMatch路径。但是,当我这样做时,会丢失浏览器URL中的错误路径。

当内部Switch组件没有在其父Switch组件中不断搜索的匹配路由时,是否有机会?

这样,我就能打出NoMatch路线,并在URL中保留错误的路径。

编辑:我更新了我的答案,下面的最终解决方案按预期工作。

const Frontend = (props) => {
  const { match } = props;
  return (<div>
    <h1>Frontend</h1>
    <p><Link to={match.path}>Home</Link></p>
    <p><Link to={`${match.path}users`}>Users</Link></p>
    <p><Link to="/admin">Admin</Link></p>
    <p><Link to={`${match.path}not-found-page`}>404</Link></p>
    <hr />
    <Switch>
      <Route exact path={match.path} component={Home} />
      <Route path={`${match.path}users`} component={Users} />
      {
      // Workaround
      }
      <Redirect to="/error" />
    </Switch>
  </div>);
};

const Admin = (props) => {
  const { match } = props;
  return (<div>
    <h1>Admin</h1>
    <p><Link to={match.path}>Dashboard</Link></p>
    <p><Link to={`${match.path}/users`}>Edit Users</Link></p>
    <p><Link to="/">Frontend</Link></p>
    <p><Link to={`${match.path}/not-found-page`}>404</Link></p>
    <hr />
    <Switch>
      <Route exact path={match.path} component={Home} />
      <Route path={`${match.path}/users`} component={Users} />
      {
      // Workaround
      }
      <Redirect to="/error" />
    </Switch>
  </div>);
};

const ErrorPage = () =>
  <div>
    <h1>404 not found</h1>
    <p><Link to="/">Home</Link></p>
  </div>;

const App = () => (
  <div>
    <AddressBar />
    <Switch>
      <Route path="/error" component={ErrorPage} />
      <Route path="/admin" component={Admin} />
      <Route path="/" component={Frontend} />
      {
      // this should render the error page
      // instead of redirecting to /error
      }
      <Route component={ErrorPage} />
    </Switch>
  </div>
);
Run Code Online (Sandbox Code Playgroud)

dat*_*oml 5

这是这种要求的最终解决方案。为了使其正常工作,我们使用位置的state属性。在内部路由的重定向中,我们将状态设置为error: true。在GlobalErrorSwitch上,我们检查状态并呈现错误组件。

import React, { Component } from 'react';
import { Switch, Route, Redirect, Link } from 'react-router-dom';

const Home = () => <div><h1>Home</h1></div>;
const User = () => <div><h1>User</h1></div>;
const Error = () => <div><h1>Error</h1></div>

const Frontend = props => {
  console.log('Frontend');
  return (
    <div>
      <h2>Frontend</h2>
      <p><Link to="/">Root</Link></p>
      <p><Link to="/user">User</Link></p>
      <p><Link to="/admin">Backend</Link></p>
      <p><Link to="/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/' component={Home}/>
        <Route path='/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

const Backend = props => {
  console.log('Backend');
  return (
    <div>
      <h2>Backend</h2>
      <p><Link to="/admin">Root</Link></p>
      <p><Link to="/admin/user">User</Link></p>
      <p><Link to="/">Frontend</Link></p>
      <p><Link to="/admin/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/admin' component={Home}/>
        <Route path='/admin/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

class GlobalErrorSwitch extends Component {
  previousLocation = this.props.location

  componentWillUpdate(nextProps) {
    const { location } = this.props;

    if (nextProps.history.action !== 'POP'
      && (!location.state || !location.state.error)) {
        this.previousLocation = this.props.location
    };
  }

  render() {
    const { location } = this.props;
    const isError = !!(
      location.state &&
      location.state.error &&
      this.previousLocation !== location // not initial render
    )

    return (
      <div>
        {          
          isError
          ? <Route component={Error} />
          : <Switch location={isError ? this.previousLocation : location}>
              <Route path="/admin" component={Backend} />
              <Route path="/" component={Frontend} />
            </Switch>}
      </div>
    )
  }
}

class App extends Component {
  render() {
    return <Route component={GlobalErrorSwitch} />
  }
}

export default App;
Run Code Online (Sandbox Code Playgroud)