'Is there an equivalent for {useLocation} from 'react-router-dom' in nextjs

import { useLocation } from 'react-router-dom';

I'm currently using Nextjs, I need help for an equivalent of useLocation

`

 import Link from 'next/link';
 import { FC } from 'react';
 import {useRouter} from 'next/router';
 import { useStore } from '../store';`



const PrivateRoute: FC = ({ children }) => {
const currentUser = useStore((state) => state.currentUser);
const location = useRouter();`

if (!currentUser)
    return (
        <Link
            href={`/sign-in?redirect=${encodeURIComponent(
                location.pathname + location.push('chat')
            )}`}
        />
    );
return <>{ children }</>

}

export default PrivateRoute;`



Solution 1:[1]

You can use useRouter from 'next/router' and then you can either use pathname or asPath and then you conditionally add slug or params. Rest you can find more information via documentation posted by others.

Solution 2:[2]

Yea you can use useNavigate in react-router-dom v6 and you can use useHistory in react-router-dom < v6

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Soheb
Solution 2 S N Sharma