英文:
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
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 '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
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目录
在其中创建一个名为404.js或404.tsx的文件
然后在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
inside it create a file called 404.js or 404.tsx
and inside 404.js write your code :
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
I just tried it and it works fine a get me back to the home page
hope i helped
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论