useLocation

useLocation

Type declaration
declare function useLocation(): Location;

interface Location extends Path {
  state: unknown;
  key: Key;
}

This hook returns the current location object. This can be useful if you'd like to perform some side effect whenever the current location changes.

import * as React from 'react';
import { useLocation } from 'react-router-dom';

function App() {
  let location = useLocation();

  React.useEffect(() => {
    // Google Analytics
    ga('send', 'pageview');
  }, [location]);

  return (
    // ...
  );
}
Docs and examples CC 4.0