如何访问当前组件中的最后路径? (React Router V6)

发布于 2025-02-09 20:31:37 字数 192 浏览 0 评论 0原文

我想根据我们到达当前页面的位置,以不同的方式渲染UI。例如,如果我们来自“/mycollection”/apisearchResults',则项目详细信息页面应具有额外的“删除”按钮。在React Router V6中如何实现这一目标?

我知道我可以创建一个更高的组件,该组件封装了重用组件和额外功能。但是我想知道我是否可以没有事事。

谢谢!

I would like to render the UI differently depending on where we arrive at the current page. For example, an item detail page should have an extra 'delete' button if we come from '/mycollection' versus '/apiSearchResults'. How do I achieve this in react router v6?

I know I can create an Higher order component that encapsulates the reused component and the extra feature. But I am wondering if I can do without an HOC.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2025-02-16 20:31:37

您可以使用< link>组件传递状态,在这种情况下,请访问pathName:

<Link to={{pathname: '/nextpath', state: { prevPath: location.pathname }}}>Example Link</Link>

然后,您可以从this.props.location.state 访问prevather < /代码>在下一个组件中

You can pass down state using the <Link> component, in this case a pathname:

<Link to={{pathname: '/nextpath', state: { prevPath: location.pathname }}}>Example Link</Link>

You can then access prevPath from this.props.location.state in the next component

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文