4

How does one get the current route for reach router. In react router one would get that through the params?

The docs don't currently show an example how to do this.

chrisjlee
  • 18,399
  • 22
  • 72
  • 104

3 Answers3

3

Alternatively, use the useLocation hook (since 1.3.0):

import { useLocation } from "@reach/router"

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

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

Ref: https://reach.tech/router/api/useLocation

ptim
  • 12,355
  • 8
  • 70
  • 89
1

Use the this.props.location as it's passed down to the component:

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

chrisjlee
  • 18,399
  • 22
  • 72
  • 104
0

You can simply just import useLocation from @reach/router and assign it to a const use it in your code as below.

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;