英文:
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:
<DefaultRoute path={['/home', '/contact']} component={NavbarButtons} /> <-- Keep adding for each new page
<DefaultRoute path={'/home'} component={Home} />
<DefaultRoute path={'/contact'} component={Contact} />
<DefaultRoute path={'/no-nav-page'} component={NoNavPage} />
I'd like to achieve this:
<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} />
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 (
<>
{routeVars.hideTopbar.includes(pathname) ? null : <TopBar />}
</>
)
But that becomes overcomplicated when using paths like "/page/:id"
.
答案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>
英文:
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 "react-router-dom";
const InverseRoute = ({ hidePath, ...props }) => {
const hide = useRouteMatch(hidePath);
return hide ? null : <Route {...props} />;
};
<InverseRoute
path="*" // <-- match anything
hidePath={["/no-nav-page"]} // <-- hide on these matches
component={NavbarButtons}
/>
<Switch>
<Route path="/home" component={Home} />
<Route path="/contact" component={Contact} />
<Route path="/no-nav-page" component={NoNavPage} />
</Switch>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论