[英]After Next.js exporting static html, how can I judge whether to jump through Link or native a tag jump?
Condition: Export static html状态:出口 static html
How to recognize whether to jump by next/link
or by direct access
(that is, a tag access)?如何识别是通过next/link
跳转还是direct access
(即标签访问)?
I tried using pageProps
with useRouter
and _app.tsx
, but both failed.我尝试将pageProps
与useRouter
和_app.tsx
一起使用,但都失败了。
Help Me!帮我!
Translated by Google由谷歌翻译
Since the Next/App
component will not be unmounted for client-side transitions, you can track the previous asPath
provided by the useRouter
hook inside the App
component exported from _app.tsx
.由于不会为客户端转换卸载Next/App
组件,因此您可以跟踪从_app.tsx
导出的App
组件内的useRouter
挂钩提供的先前asPath
。 If there is no previous asPath
, then it is not client-side transitions.如果之前没有asPath
,那么它不是客户端转换。
asPath
:String
- Actual path (including the query) shown in the browserasPath
:String
- 浏览器中显示的实际路径(包括查询)
Possible implementation:可能的实施:
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
function MyApp({ Component, pageProps }) {
const { asPath } = useRouter();
const previousAsPath = usePrevious(asPath);
useEffect(() => {
if (!previousAsPath) {
console.log('Direct access');
} else {
console.log(`Come from ${previousAsPath}`);
}
}, [previousAsPath]);
return <Component {...pageProps} />
}
export default MyApp;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.