创建反向的 react-router 路径;在匹配时隐藏。

huangapple go评论73阅读模式
英文:

Create inverse react-router <Route /> path; hide on match

问题

我想要一种通过路径反转路由的方法,而不是像标准行为一样使用 include。

而不是这样做:

<DefaultRoute path={['/home', '/contact']} component={NavbarButtons} /> <-- 为每个新页面添加
<DefaultRoute path={'/home'} component={Home} />
<DefaultRoute path={'/contact'} component={Contact} />
<DefaultRoute path={'/no-nav-page'} component={NoNavPage} />

我想要实现这样的效果:

<DefaultRoute path={"*"} hidePath={['/no-nav-page']} component={NavbarButtons} />
<DefaultRoute path={'/home'} component={Home} />
<DefaultRoute path={'/contact'} component={Contact} />
<DefaultRoute path={'/no-nav-page'} component={NoNavPage} />

当您不断添加新页面,但希望在 NavbarButtons 显示时将它们包括在默认页面集中时,这将特别有用,而不必手动为每个新页面添加 path 定义。

我可以使用 useLocation 手动匹配路径并有条件地呈现组件:

const { pathname } = useLocation()

return (
  <>
   {routeVars.hideTopbar.includes(pathname) ? null : <TopBar />}
  </>
)

但当使用像 "/page/:id" 这样的路径时,这变得过于复杂。

英文:

I would like a way to invert a Route by path, instead of include like the standard behavior.

Instead of doing:

&lt;DefaultRoute path={[&#39;/home&#39;, &#39;/contact&#39;]} component={NavbarButtons} /&gt; &lt;-- Keep adding for each new page
&lt;DefaultRoute path={&#39;/home&#39;} component={Home} /&gt;
&lt;DefaultRoute path={&#39;/contact&#39;} component={Contact} /&gt;
&lt;DefaultRoute path={&#39;/no-nav-page&#39;} component={NoNavPage} /&gt;

I'd like to achieve this:

&lt;DefaultRoute path={&quot;*&quot;} hidePath={[&#39;/no-nav-page&#39;]} component={NavbarButtons} /&gt;
&lt;DefaultRoute path={&#39;/home&#39;} component={Home} /&gt;
&lt;DefaultRoute path={&#39;/contact&#39;} component={Contact} /&gt;
&lt;DefaultRoute path={&#39;/no-nav-page&#39;} component={NoNavPage} /&gt;

This becomes especially useful when you keep adding new pages, but want to include them in the default set of pages when the NavbarButtons show up, and not manually have to add a path definition for each new page.

I could use useLocation to match the path manually and conditionally render a component:

const { pathname } = useLocation()

return (
  &lt;&gt;
   {routeVars.hideTopbar.includes(pathname) ? null : &lt;TopBar /&gt;}
  &lt;/&gt;
)

But that becomes overcomplicated when using paths like &quot;/page/:id&quot;.

答案1

得分: 1

指定一个包含路由的 NavbarButtons 组件要渲染的方式最初是有条件地渲染路由内容的标准传统方式,但如果你想要相反的效果,你需要一个路由来无条件地匹配任何内容,然后显式地检查一组路由来独占地渲染内容。

以下示例使用 useRouteMatch 钩子来匹配一组 "hidepath" 路径。如果存在匹配,那么自定义的 InverseRoute 组件将返回 null 以“隐藏”路由内容,否则它将返回一个带有正常路由属性的常规 Route。这种模式只是一种专门的路由保护实现。

示例:

import { Route, useRouteMatch } from "react-router-dom";

const InverseRoute = ({ hidePath, ...props }) => {
  const hide = useRouteMatch(hidePath);

  return hide ? null : <Route {...props} />;
};
<InverseRoute
  path="*"                    // <-- 匹配任何内容
  hidePath={["/no-nav-page"]} // <-- 在这些匹配上隐藏
  component={NavbarButtons}
/>
<Switch>
  <Route path="/home" component={Home} />
  <Route path="/contact" component={Contact} />
  <Route path="/no-nav-page" component={NoNavPage} />
</Switch>

创建反向的 react-router <Route /> 路径;在匹配时隐藏。

英文:

The initial way of specifying an inclusive set of routes you want the NavbarButtons component to render with is the standard conventional way of conditionally rendering routed content, but if you wanted to do the inverse you would need a route that unconditionally matched anything and then explicitly checked a set of routes to exclusively render content on.

The following example uses the useRouteMatch hook to match a set of "hidepath" paths. If there is a match then the custom InverseRoute component returns null to "hide" the routed content, otherwise it returns a regular Route with the regular route props passed through. This pattern is just a specialized route protection implementation.

Example:

import { Route, useRouteMatch } from &quot;react-router-dom&quot;;

const InverseRoute = ({ hidePath, ...props }) =&gt; {
  const hide = useRouteMatch(hidePath);

  return hide ? null : &lt;Route {...props} /&gt;;
};
&lt;InverseRoute
  path=&quot;*&quot;                    // &lt;-- match anything
  hidePath={[&quot;/no-nav-page&quot;]} // &lt;-- hide on these matches
  component={NavbarButtons}
/&gt;
&lt;Switch&gt;
  &lt;Route path=&quot;/home&quot; component={Home} /&gt;
  &lt;Route path=&quot;/contact&quot; component={Contact} /&gt;
  &lt;Route path=&quot;/no-nav-page&quot; component={NoNavPage} /&gt;
&lt;/Switch&gt;

创建反向的 react-router <Route /> 路径;在匹配时隐藏。

huangapple
  • 本文由 发表于 2023年2月19日 21:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定