Trying to make router.push work (beginner)
Answered
Minskin posted this in #help-forum
MinskinOP
Hello, friends,
I'm desperately trying to get my nextjs router to take me to a page I created.
My page is in app/pages/ArticlePage.tsx
And so I want this button from my main page.tsx to redirect to the Article page
but I have a 404 error, page not found :/
How to fix this ? thank you
I'm desperately trying to get my nextjs router to take me to a page I created.
My page is in app/pages/ArticlePage.tsx
export function ArticlePage() {
return (...)
}And so I want this button from my main page.tsx to redirect to the Article page
<button className="..."
onClick={() => router.push("/article")}>
Access the resources
</button>but I have a 404 error, page not found :/
How to fix this ? thank you
Answered by Minskin
I made it works by putting the pages folder in the app directory and not in app/src
8 Replies
Siberian
I'd recommend you read through the docs on routing and such
the issues here are that routes are defined by folder names, and if you want them to have a Page you need to add a page.tsx (filename needs to be Page), That would be app/pages/article/page.tsx in this case.
also that page needs to default export a react component
so
export default function instead of export function@Siberian the issues here are that routes are defined by folder names, and if you want them to have a Page you need to add a page.tsx (filename needs to be Page), That would be app/pages/article/page.tsx in this case.
MinskinOP
but so now I have to type www.localhost:3000/pages/article and not www.localhost:3000/article
MinskinOP
I made it works by putting the pages folder in the app directory and not in app/src
Answer
Siberian
ooh sorry yea I thought you wanted to have /pages/article as an URL, if you want to use the pages router it does indeed need to be in the same directory as app (so either root or src)