How to route Components like in React?
Answered
Jumbo flying squid posted this in #help-forum
Jumbo flying squidOP
So I have been away from React since a year for Job requirements reasons, now I learned about Nextjs a week ago and I did read a good share of the documents , but I still cant figure this out somehow, I used to route components using Route and Switch in Reactjs, but after asking bard AI (a bad idea tbh) and tries the suggested code I cant seem to make it work. Kindly check the files I shared, simple code actually , what am I doing wrong ? Is it not possible anymore? or am I missing something?
thanks a lot in advance!
thanks a lot in advance!
Answered by English Springer Spaniel
If you're using the app directory, page.js is just the file name to identify a page. The folder names are what allow you to define dynamic routes, catch-all routes, layout groups, parallelized routing and so forth. If a folder doesn't have a page.js file as a direct child of that folder then it won't be considered a route. Furthermore, with the new app router you can include components and folders within the app directory as long as their namespace isn't taken. I hope that addresses your question here.
As far as the old approach is concerned, I'm not too sure. I'm unfamiliar with the code you've pasted in your example, which leads me to believe it's older than me or that perhaps the code (if generated) was coming from the React-Router package which handles client side routing.
If it's not mentioned in the docs, then It's my understanding, unless you download older versions of nextjs, then you cannot do what you're trying to do exactly as you've provided in your original example.
As far as the old approach is concerned, I'm not too sure. I'm unfamiliar with the code you've pasted in your example, which leads me to believe it's older than me or that perhaps the code (if generated) was coming from the React-Router package which handles client side routing.
If it's not mentioned in the docs, then It's my understanding, unless you download older versions of nextjs, then you cannot do what you're trying to do exactly as you've provided in your original example.
9 Replies
Cinnamon
If you're using NextJs why not just use <Link> component from Next and the pages folder to create new routes ? https://nextjs.org/docs/pages/building-your-application/routing/linking-and-navigating
@Cinnamon If you're using NextJs why not just use <Link> component from Next and the pages folder to create new routes ? https://nextjs.org/docs/pages/building-your-application/routing/linking-and-navigating
Jumbo flying squidOP
Is it the better approach?
I know it's straightforward and much simpler and easier to do this but is it the best approach?
I am totally new to Nextjs , please excuse my questions.
I know it's straightforward and much simpler and easier to do this but is it the best approach?
I am totally new to Nextjs , please excuse my questions.
English Springer Spaniel
@Jumbo flying squid Yes, the better approach currently is to use folder/file based routing. Now depending on how much cool tech you'd like to learn, IE server components, you could go one step better and take a look at the app directory which provides a new way of routing. I'd absolutely recommend reviewing the docs so you can see everything that is possible 😄 .
If you'd like a visual representation of how layouts and routes are rendered in the new nextjs app dir. Check out this amazing demo as well! https://app-router.vercel.app/layouts
https://nextjs.org/docs/app/building-your-application/routing
If you'd like a visual representation of how layouts and routes are rendered in the new nextjs app dir. Check out this amazing demo as well! https://app-router.vercel.app/layouts
https://nextjs.org/docs/app/building-your-application/routing
@English Springer Spaniel <@446793120743555075> Yes, the better approach currently is to use folder/file based routing. Now depending on how much cool tech you'd like to learn, IE server components, you could go one step better and take a look at the app directory which provides a new way of routing. I'd absolutely recommend reviewing the docs so you can see everything that is possible 😄 .
If you'd like a visual representation of how layouts and routes are rendered in the new nextjs app dir. Check out this amazing demo as well! https://app-router.vercel.app/layouts
https://nextjs.org/docs/app/building-your-application/routing
Jumbo flying squidOP
Thanks , I tried the Link approach and seems working, i still wonder if I can use the old approach or not though, seemed more organized, also can I add more than page.js to my folders? or by doing that I will have to make route dynamic like [slug].js for example?
@English Springer Spaniel <@446793120743555075> Yes, the better approach currently is to use folder/file based routing. Now depending on how much cool tech you'd like to learn, IE server components, you could go one step better and take a look at the app directory which provides a new way of routing. I'd absolutely recommend reviewing the docs so you can see everything that is possible 😄 .
If you'd like a visual representation of how layouts and routes are rendered in the new nextjs app dir. Check out this amazing demo as well! https://app-router.vercel.app/layouts
https://nextjs.org/docs/app/building-your-application/routing
Jumbo flying squidOP
Also Nice Portfolio by the way, I plan to do mine with Next js after learning it and creating my web app 😄
@Jumbo flying squid Thanks , I tried the Link approach and seems working, i still wonder if I can use the old approach or not though, seemed more organized, also can I add more than page.js to my folders? or by doing that I will have to make route dynamic like [slug].js for example?
English Springer Spaniel
If you're using the app directory, page.js is just the file name to identify a page. The folder names are what allow you to define dynamic routes, catch-all routes, layout groups, parallelized routing and so forth. If a folder doesn't have a page.js file as a direct child of that folder then it won't be considered a route. Furthermore, with the new app router you can include components and folders within the app directory as long as their namespace isn't taken. I hope that addresses your question here.
As far as the old approach is concerned, I'm not too sure. I'm unfamiliar with the code you've pasted in your example, which leads me to believe it's older than me or that perhaps the code (if generated) was coming from the React-Router package which handles client side routing.
If it's not mentioned in the docs, then It's my understanding, unless you download older versions of nextjs, then you cannot do what you're trying to do exactly as you've provided in your original example.
As far as the old approach is concerned, I'm not too sure. I'm unfamiliar with the code you've pasted in your example, which leads me to believe it's older than me or that perhaps the code (if generated) was coming from the React-Router package which handles client side routing.
If it's not mentioned in the docs, then It's my understanding, unless you download older versions of nextjs, then you cannot do what you're trying to do exactly as you've provided in your original example.
Answer
@Jumbo flying squid Also Nice Portfolio by the way, I plan to do mine with Next js after learning it and creating my web app 😄
English Springer Spaniel
Ahahaha, thanks, I want to redo it because at this point it's fairly old. I want to take advantage of all the new cool features nextjs 13 offers and more. That's a project for another day though 😆
@English Springer Spaniel Ahahaha, thanks, I want to redo it because at this point it's fairly old. I want to take advantage of all the new cool features nextjs 13 offers and more. That's a project for another day though 😆
Jumbo flying squidOP
Yes you did clarify most of my questions and yes It was from react-router and react-dom , It was back in 2021 so yeah quite old , here are tow screenshots of how I used to write the code if that clarify anything 😆
Thanks a lot for your great help! 😄
Thanks a lot for your great help! 😄
@Jumbo flying squid Yes you did clarify most of my questions and yes It was from react-router and react-dom , It was back in 2021 so yeah quite old , here are tow screenshots of how I used to write the code if that clarify anything 😆
Thanks a lot for your great help! 😄
English Springer Spaniel
I'm glad I could help! Happy coding
!
!