@reach/router: how to get the current route / page?
Asked Answered
D

3

5

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.

Deserved answered 1/7, 2019 at 15:7 Comment(1)
In the this.props.location in your component you have your location details including the current location.Swedenborgianism
D
3

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

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

Deserved answered 1/7, 2019 at 15:12 Comment(0)
J
4

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

Jard answered 18/7, 2020 at 14:39 Comment(0)
D
3

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

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

Deserved answered 1/7, 2019 at 15:12 Comment(0)
A
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;
Apotheosize answered 17/12, 2020 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.