0

I am facing an issue with routing and links in Nextjs. I have created a blog like page using React+Typescript and I would like to use the same code in Nextjs. I was initially using import {Link} from 'react-router-dom'; .However since this link doesn't work with Nextjs, I have used the link/next import.

The issue is when, I try to click on "Aboutus" of my Navbar, I get the following error:

404 This page could not be found.

Here is the code of my Navbar.tsx

import React from 'react';
import Link from 'next/link';
const Navbarr : React.FC = () => {
return (
<div>
<AppName >
        <Link href="/"><a>Abc</a></Link>
    </AppName>
    <Button>
        <Link href="/new">
          <a>Aboutus</a>
        </Link>
    </Button>
</div>)}
export default NavBarr;
1
  • you have to add all pages at pages directory to get a route link Commented Feb 18, 2020 at 16:13

1 Answer 1

2

In Next.js when a file is added to the pages directory it's automatically available as a route.

I suggest you to read https://nextjs.org/docs/routing/introduction

Sign up to request clarification or add additional context in comments.

6 Comments

I have added both Navbar.tsx and App.tsx in the pages folder only. Here is the screenshot prnt.sc/r45pif
Please check the screenshot above, all the route files mentioned in App.tsx are in the pages folder
<Link href="/new"> doesn't mach with NewQuestion.tsx. Try to rename the file or the route eg. new.tsx
I have renamed <Link href="/new"> to <Link href="/NewQuestion"> but then what is the point of routing in this? In my app.tsx I have routed /new to NewQuestion
Have a check on this nextjs.org/docs/api-reference/next/link You are probably gonna find everything you need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.