【问题标题】:react-router show different when component is wraped包装组件时反应路由器显示不同
【发布时间】:2020-03-06 17:54:47
【问题描述】:

我是新的 react 用户,当我看到 react-router 文档时,我很困惑。
让我展示一下,
首先,文档网址:https://reacttraining.com/react-router/web/example/route-config
我这样简化

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

const routes = [
  {
    path: "/",
    component: Sandwiches
  },
  {
    path: "/tacos",
    component: Tacos
  }
];

export default function RouteConfigExample() {
  return (
    <Router>
      <div>
        <ul>
          <li>
            <Link to="/">/</Link>
          </li>
          <li>
            <Link to="/tacos">tacos</Link>
          </li>
        </ul>

        <Switch>
          {routes.map((route, i) => (
            <RouteWithSubRoutes key={i} {...route} />
          ))}
        </Switch>
      </div>
    </Router>
  );
}
function RouteWithSubRoutes(route) {
  return (
    <Route
      exact
      path={route.path}
      component={route.component}
    />
  );
}

function Sandwiches() {
  return <h2>/</h2>;
}

function Tacos() {
  return (
    <div>
      <h2>Tacos</h2>
    </div>
  );
}

现在,当我单击 / 显示 /,但单击 Tacos 时什么都不显示。
期待什么,单击/显示/,然后单击 Tacos 显示 Tacos。
我通过这些解决了

// first, do not use component wrap

export default function RouteConfigExample() {
  return (
    <Router>
      <div>
        <ul>
          <li>
            <Link to="/">/</Link>
          </li>
          <li>
            <Link to="/tacos">tacos</Link>
          </li>
        </ul>

        <Switch>
          {routes.map((route, i) => (
            <Route
              key={i}
              exact
              path={route.path}
              component={route.component}/>
          ))}
        </Switch>
      </div>
    </Router>
  );
}
// second, do not use Switch
export default function RouteConfigExample() {
  return (
    <Router>
      <div>
        <ul>
          <li>
            <Link to="/">/</Link>
          </li>
          <li>
            <Link to="/tacos">tacos</Link>
          </li>
        </ul>

        {routes.map((route, i) => (
            <RouteWithSubRoutes key={i} {...route} />
        ))}
      </div>
    </Router>
  );
}

困扰了我很久,现在很想知道为什么,请详细告诉我,非常感谢...
另外,我的英文不太好,懂...

【问题讨论】:

标签: react-router react-router-dom


【解决方案1】:

Switch 组件只有有效的子组件是 RouteRedirect,因此即使另一个 react 组件呈现 Route,它也不会以相同的方式工作。

Switch children

&lt;Switch&gt; 的所有子元素都应该是 &lt;Route&gt;&lt;Redirect&gt; 元素。 只有第一个匹配当前位置的子节点才会被渲染。

编辑

虽然包装的 Route 组件可以仍然呈现,作为“grand-children”后代,但似乎路由道具仅应用于根 Switch 级别.

在下面的Switch中,虽然RouteWithSubRoutes指定了exact这个prop,但是react DOM中的RouteWithSubRoutes没有,所以只返回了第一个元素(正好home "/" 路由)。

<Switch>
  {routes.map(route => (
    <RouteWithSubRoutes key={route.path} {...route} />
  ))}
</Switch>

下面的Switch 与上面的相同,只是指定了exact 属性,并且按预期工作。

<Switch>
  {routes.map(route => (
    <RouteWithSubRoutes key={route.path} exact {...route} />
  ))}
</Switch>

/编辑

听起来您有一个由两部分组成的问题,为什么两种解决方案都有效。

通过在Switch 内的所有路由上指定exact 属性直接通过渲染Route 进行修复的第一次尝试成功,它只匹配并渲染第一个 匹配RouteRedirect。当路径为 exactly "/" 时,该组件会呈现,而当路径是 exactly "/tacos" 时,则该组件会呈现。

第二次尝试在Router 中渲染所有路由,它匹配并渲染所有 匹配路由,但是由于您再次指定exact 属性,它匹配一个单一路线和作品。

演示

【讨论】:

  • Switch组件只有有效的子组件是Route和Redirect?我可能知道...但是我很困惑,在我的简化示例中,为什么打包的Route 与未打包的Route 不同,请您详细解释一下,非常感谢
  • @liukun 添加了一个“编辑”部分来解释您的后续问题。我还用更多的演示更新了代码沙箱。
  • 我看到了……你真好。但我还有其他问题。首先,为什么使用反向有意义,将路径'/'放在底部是有效的。其次,我想包装一个多级菜单,如何递归地封装路由,如后台管理系统侧菜单。如果你给我演示一下,对我来说很重要。
  • 以及如何在不刷新的情况下跳转到一个页面?喜欢 vue.js 吗?使用最新的反应 API
  • 使用 hashHistory?但我没有看到这个细节......出口?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 2019-06-20
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
相关资源
最近更新 更多