How to Get the Current Page URL in Gatsby

Updated: February 5, 2021

Gatsby uses Reach Router under the hood to handle routing. If you want to provide location anywhere in your app, you need to use the React Router's Location Component.

Create a page inside the src/pages folder:

import React from "react";
import { Location } from "@reach/router";

import Layout from "../components/layout";
import RandomComponent from "../components/randomComponent";

const RandomPage = () => (
{(locationProps) => <RandomComponent {...locationProps} />}

export default RandomPage;

Create a component inside the src/components folder:

import React from "react";

const RandomComponent = ({ location }) => (

export default RandomComponent;