logo@krutoo/utils

useLocation

React hook of state of current location provided by router.

Usage

import { useLocation } from '@krutoo/utils/react';

function ProfilePage() {
  const { pathname, hash, search } = useLocation();

  return <>{/* ... */}</>;
}

Requirements

You need to wrap your root component to special RouterContext to make router specific hooks working:

import { createRoot } from 'react-dom';
import { RouterContext } from '@krutoo/utils/react';
import { BrowserRouter } from '@krutoo/utils/router';
import { App } from '#components/app';

const router = new BrowserRouter();

router.connect();

createRoot(document.querySelector('#root')).render(
  <RouterContext value={router}>
    <App />
  </RouterContext>,
);