已解决:反应获取当前路线

React 获得当前路线的主要问题是它可能很慢。

There is no built-in way to get the current route in React. However, you can use the router's context to get the current location, which will give you the current pathname.

const { location } = this.context.router; console.log(location.pathname); // /some/path

姓名

使用location()钩子

uselocation() 钩子用于确定组件是否应在根目录中呈现。 如果组件不应该在根目录下呈现,它可以使用这个钩子来确定它应该在哪里呈现。

如何获取路线

在 React 中有几种获取路由的方法。 一种方法是使用反应路由器。 另一种方法是使用 react-router-dom。

相关文章:

发表评论