链接在notFound组件中无法正常工作,nextjs 13.4版本。

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

Link not working in notFound component nextjs 13.4

问题

I created a not-found.tsx component in my app directory in nextjs 13.4 which works fine when I enter wrong route as seen below:

import Link from 'next/link'

function NotFound() {
  
  return (
    <section>
      
      404, page not found
      <br/>
      <Link href="/">Back to HomePage</Link>

    </section>
  )
}

export default NotFound

But when I click the link to go back to the homepage, I still get the 404 page not found page.

I tried using the useRouter to navigate away but got the same problem. I notice the issue is as a result of not-found being in the app directory with the homepage

链接在notFound组件中无法正常工作,nextjs 13.4版本。

So how do I navigate from the not found page to the home page?

英文:

I created a not-found.tsx component in my app directory in nextjs 13.4 which works fine when I enter wrong route as seen below:

import Link from &#39;next/link&#39;

function NotFound() {
  
  return (
    &lt;section&gt;
      
      404, page not found
      &lt;br/&gt;
      &lt;Link href=&quot;/&quot;&gt;Back to HomePage&lt;/Link&gt;

    &lt;/section&gt;
  )
}

export default NotFound

But when I click the link to go back to the homepage, I still get the 404 page not found page.

I tried using the useRouter to navigate away but got the same problem. I notice the issue is as a result of not-found being in the app directory with the homepage

链接在notFound组件中无法正常工作,nextjs 13.4版本。

So how do I navigate from the not found page to the home page?

答案1

得分: 1

这是Next.js 13.4中的一个错误,已经修复,请安装最新版本的Next.js npm install next@latest react@latest react-dom@latest。一切都应该正常工作。

英文:

this was a bug in nextjs 13.4 and it has been fixed, just install the latest version of next npm install next@latest react@latest react-dom@latest. and everything should work fine.

答案2

得分: 0

In Next.js,您需要在pages目录下创建一个名为404.js或404.tsx的文件才能使404页面正常工作。

如下所示:

这是pages目录

链接在notFound组件中无法正常工作,nextjs 13.4版本。

在其中创建一个名为404.js或404.tsx的文件

链接在notFound组件中无法正常工作,nextjs 13.4版本。

然后在404.js中编写您的代码:

import Link from "next/link"

function PageNotFound(){
  return (
    <section>
      <h1>PAGE NOT FOUND </h1>
      <Link href="/"> BACK TO HOME PAGE</Link>
    </section>
  )
}

export default PageNotFound

我刚刚尝试了它,它可以正常工作并将我带回主页。

希望有所帮助。

英文:

In nextjs you have to create a file called 404.js or 404.tsx in the pages directory for 404 to works

like this :

this is the pages directory

链接在notFound组件中无法正常工作,nextjs 13.4版本。

inside it create a file called 404.js or 404.tsx

链接在notFound组件中无法正常工作,nextjs 13.4版本。

and inside 404.js write your code :

import Link from &quot;next/link&quot;

function PageNotFound(){
  return (
    &lt;section&gt;
      &lt;h1&gt;PAGE NOT FOUND &lt;/h1&gt;
      &lt;Link href=&quot;/&quot;&gt; BACK TO HOME PAGE&lt;/Link&gt;
    &lt;/section&gt;
 )
}

export default PageNotFound

I just tried it and it works fine a get me back to the home page

hope i helped

huangapple
  • 本文由 发表于 2023年5月20日 23:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295977.html
匿名

发表评论

匿名网友

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

确定