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

Time:03-08

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;`

CodePudding user response:

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

CodePudding user response:

check out the next.js documentation of react-router-dom

CodePudding user response:

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.

  • Related