【问题标题】:Nesting Switch components with global NoMatch component in react-router-v4在 react-router-v4 中使用全局 NoMatch 组件嵌套 Switch 组件
【发布时间】:2017-07-25 04:31:08
【问题描述】:

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

  • 前端
  • 管理
  • 错误

前端、管理和错误组件都有自己的样式。

前端和管理组件也有自己的 Switch 组件来浏览它们。

我面临的问题是我无法在没有 Redirect 组件的情况下访问 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>
);

【问题讨论】:

  • 你是如何引入应用程序、管理员等的?
  • App 是主要的反应组件,如果这就是你的意思的话。
  • 看来问题是您应该将exact 添加到App.js 中的索引路径&lt;Route exact path="/" component={Frontend} /&gt;。问题是它正在捕获所有请求,因此您的错误路线永远不会呈现。
  • 这是我的第一个解决方案。但是,当您将 exact 添加到第一个 Route 时,内部的 Route 将无法再访问。

标签: reactjs react-router react-router-v4


【解决方案1】:

这是此类需求的最终解决方案。 为了使它工作,我们使用该位置的状态属性。在内部路由的重定向中,我们将状态设置为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;

【讨论】:

  • 不错的解决方案,但我认为您不需要 previousLocation 逻辑。
  • 我可以确认这种方法效果很好。 (“反应”:“^16.7.0”,“反应路由器”:“^4.3.1”)
  • 很高兴听到。 ?
【解决方案2】:

所有子组件的路由都包裹在父组件的&lt;Switch&gt; 中(switch 内部的app 组件)你实际上并没有在子组件中切换。

只需删除子 switch.component 并让 &lt;App &lt;Switch&gt; 中的 404 捕获任何缺失。

【讨论】:

  • 当我在 App 中移除 组件时,它也不起作用。
  • 你能发布一个代码笔来展示一个不工作的例子吗?
  • 查看您的更新,您似乎已按照我的回答(对您有用)删除了开关。您关于前导斜线不起作用的新问题是一个新的且不相关的问题...
  • 并不是因为 / 一开始是我的问题的一部分。谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 2017-10-05
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多