@ reach / router:如何获取当前路线/页面?

时间:2019-07-01 15:07:12

标签: reactjs reach-router

如何获取到达路由器的当前路由。在反应路由器中,可以通过参数实现这一点吗?

该文档当前未显示如何执行此操作的示例。

3 个答案:

答案 0 :(得分:3)

或者,使用useLocation钩子(自1.3.0起):

import { useLocation } from "@reach/router"

const useAnalytics = () => {
    const location = useLocation()

    useEffect(() => {
        ga.send(['pageview', location.pathname])
    }, [])
}

参考:https://reach.tech/router/api/useLocation

答案 1 :(得分:0)

使用this.props.location传递给组件:

https://reach.tech/router/api/Router

答案 2 :(得分:0)

您只需从useLocation导入@reach/router并将其分配给一个const,就可以在您的代码中使用它,如下所示。

import React from "react";

import { useLocation } from "@reach/router";

const YourPage = () => {
  const location = useLocation();
  return (
    <Page>
      <div>{(location.pathname = "/terms/")}</div>
    </Page>
  );
};

export default YourPage;
相关问题