Tem*_*odu 12 javascript reactjs next.js
import { useLocation } from 'react-router-dom';
我目前正在使用 Nextjs,我需要类似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 }</>
Run Code Online (Sandbox Code Playgroud)
}
导出默认私有路由;`
const router = useRouter()
const navigateTo = () => router.push('/dashboard/app')
Run Code Online (Sandbox Code Playgroud)
const router = useRouter()
console.log(router.pathname); // logs the current pathname
Run Code Online (Sandbox Code Playgroud)
页A:
const router = useRouter()
router.push({
pathname: '/B',
query: { result: JSON.stringify(result) }
});
Run Code Online (Sandbox Code Playgroud)
B页:
const router = useRouter()
let result = JSON.parse(router.query.result);
Run Code Online (Sandbox Code Playgroud)