Shallow router push in Next.js 13 with appDir enabled

huangapple go评论105阅读模式
英文:

Shallow router push in Next.js 13 with appDir enabled

问题

In < Next 13(或禁用 appDir)中,您可以执行以下操作:

  1. const MyComponent = () => {
  2. const router = useRouter();
  3. const toggleStatic = () => {
  4. if (router.query.static) {
  5. router.push(router.pathname, router.pathname, { shallow: true });
  6. } else {
  7. router.push(router.pathname, router.pathname + "static", { shallow: true });
  8. }
  9. }
  10. return <>;
  11. // ...
  12. </>;
  13. });

这将执行浅层路由更新,会更改位置,但不会将其推送到历史记录或触发页面加载。

现在,启用了 appDir,您需要从 next/navigation 导入函数。但是文档并未提到如何使用新路由进行浅层路由推送?

我只能做到这样:

  1. const router = useRouter();
  2. const pathname = usePathname();
  3. const searchParams = useSearchParams();
  4. const toggleStatic = () => {
  5. if (searchParams.get("static")) {
  6. router.push(pathname);
  7. } else {
  8. router.push(pathname + "?static");
  9. }
  10. };

但这会进行完整的页面重新加载。是否有一种方法可以在 Next 13 的 appDir 中复制浅层路由的功能?

英文:

In < Next 13 (or with appDir disabled), you could do:

  1. const MyComponent = () =&gt; {
  2. const router = useRouter();
  3. const toggleStatic = () =&gt; {
  4. if (router.query.static) {
  5. router.push(router.pathname, router.pathname, { shallow: true });
  6. } else {
  7. router.push(router.pathname, router.pathname + &quot;?static&quot;, { shallow: true });
  8. }
  9. }
  10. return &lt;&gt;
  11. // ...
  12. &lt;/&gt;
  13. });

This would perform a shallow router update that would change the location, but not push it to history or trigger a page load.

Now, with appDir enabled, you need to import functions from next/navigation instead. But the docs don't say anything about shallow router pushing using the new router?

All I can do is this:

  1. const router = useRouter();
  2. const pathname = usePathname();
  3. const searchParams = useSearchParams();
  4. const toggleStatic = () =&gt; {
  5. if (searchParams.get(&quot;static&quot;)) {
  6. router.push(pathname);
  7. } else {
  8. router.push(pathname + &quot;?static&quot;);
  9. }
  10. };

But that does a full page reload. Is there a way to replicate the shallow router functionality using Next 13's appDir?

答案1

得分: 1

尝试使用replace方法,而不是push

  1. const router = useRouter();
  2. const pathname = usePathname();
  3. const searchParams = useSearchParams();
  4. const toggleStatic = () => {
  5. if (searchParams.get("static")) {
  6. router.replace(pathname);
  7. } else {
  8. router.replace(pathname + "?static");
  9. }
  10. };

在Next.js文档中关于useRouter的说明

英文:

Try the replace method instead of push:

  1. const router = useRouter();
  2. const pathname = usePathname();
  3. const searchParams = useSearchParams();
  4. const toggleStatic = () =&gt; {
  5. if (searchParams.get(&quot;static&quot;)) {
  6. router.replace(pathname);
  7. } else {
  8. router.replace(pathname + &quot;?static&quot;);
  9. }
  10. };

useRouter in nextjs docs.

huangapple
  • 本文由 发表于 2023年4月13日 23:55:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定