Next.js Discord

Discord Forum

How Do I set up Dynamic Router folder and file names and brackets

Answered
Welsh Terrier posted this in #help-forum
Open in Discord
Welsh TerrierOP
Hi, Trying to get #Routes working. Started with Router, then that was deprecated. Trying to get #Link working, getting irrelevant errors, and won't compile.

<Link href="/usa">usa</Link> results in 404 error.
Have nation > [nation] > state > [state] > county > [county] file folder structure with page.tsx file in each folder.
page.tsx is :
export default function news({
params,
}: {params: {
federalId: string };
}) {
return <h1>List of News items by { params.federalId } </h1>;
}

It's not creating dynamic pages based on params.
Answered by gin
do u want me to help out via anydesk?
View full answer

144 Replies

Welsh TerrierOP
This is where the confusion is. So the folder name is nation, but I need to rename the page.ts to [nation].ts?? I delete the folder named [nation] ?? Does it have to be a .js file or can I use .ts?
Barbary Lion
@Welsh Terrier .tsx not .ts, the x after ts makes it a typescript xml file which basically means you can use html/react elements
.ts is good for backend code because you’re not using any html elements in the backend
@Barbary Lion <@630949041278156819> .tsx not .ts, the x after ts makes it a typescript xml file which basically means you can use html/react elements
Welsh TerrierOP
So I'm assuming I need the tsx because this is UI code and I might need html, and this is not the database or API layer. Besides, Next has combined server/client code into one app, so this is all jumbled up anyway. Seems like somebody tossed the rule book "out the winder."
everything is serversided by default
"use client" directive puts it into client
rest is server only
@gin no its easy
Welsh TerrierOP
LOL, "its easy" ... aaaaaaand '404' again... 😢
Barbary Lion
Server/client components are not the same as server files. Server components are server side which means they’re rendered on the server first. If you add “use server” in a server file it ensures the backend code is run on the server side, but it’s not the same as a server component.
Barbary Lion
I’m going to find this video that details how it works better
u mean rsc
@gin "use server" is for server actions
Barbary Lion
that’s what I just said
which is default for tsx files
@Barbary Lion that’s what I just said
server actions are not rsc
Barbary Lion
I was referring to ts files
u can call server actions from the client which u can not with rsc
this is the difference
@Welsh Terrier I see you are new to react and nextjs right?
check this
But right now you’re linking to /usa as if its setup as /[nation].tsx in your file manager which you said it isn’t
https://youtu.be/5QP0mvrJkiY?si=TJacZUjTM8CWX4BT

This video is great and helped me when I first started learning nextjs. I personally hate reading docs and I’m a much better visual learner so I recommend this video/channel
@gin <@630949041278156819> I see you are new to react and nextjs right?
Welsh TerrierOP
Yes and no. I was a .net/SQL developer for years, now learning javascript, and it's like the wild west out here. VERY difficult to learn, tutorials are obsolete almost right away and the stuff I happen to need keeps getting deprecated and I have to start over again with something new. It's been ... excruciating... and the documentation is terrible, no offense intended.... Such as, I just find that <link> has an old version and a new version and Eff if I know which one I'm using or I need to use. Yet another rabbit hole I haven't yet visited. .... but I digress...
...
I'm also making the assumption that I really don't care at this point what is "server-side" or "client-side" if this doesn't even compile, right??? That's a performance thing later and waaaay over my head right now.
Barbary Lion
Just focus on using next/navigation package for everything, I agree it’s a bit confusing at first.
@gin ima help you
Welsh TerrierOP
what route shows a 404?
Barbary Lion
Just link to /nation/usa in your button not /usa, problem solved.
Welsh TerrierOP
Barbary Lion
Change the link and see if that works
Welsh TerrierOP
@Welsh Terrier Click to see attachment
u dont have a route named usa
Welsh TerrierOP
your [nation].tsx is wrong
@Welsh Terrier Click to see attachment
still wrong
is usa your dynamic id?
Welsh TerrierOP
this is how it should look like ->
/nation/[id]/page.tsx
as described in the docs
u can further extend this using catch all segments
which automatically matches multiple dynamic routes
so at the end u can have everything as a dynamic route -> /usa/nyc/blablabla
@gin this is how it should look like -> /nation/[id]/page.tsx
Welsh TerrierOP
This is how the href should look or the folder structure names?
ignore the href
its not the issue
also,
your app/ folder is strictly for routing
Welsh TerrierOP
yup
this will match everything under /nation/<id>
@gin also, your app/ folder is strictly for routing
Welsh TerrierOP
I thought the code in each page.tsx in each folder controls the UI for each folder???
@Welsh Terrier I thought the code in each page.tsx in each folder controls the UI for each folder???
page.tsx, layout.tsx and so on are conventions for routing only
so everything else doesnt belong in there
Welsh TerrierOP
Ah. Progress!
good job, but i kindly tell u again that u should read the docs link i send above
it summarizes everything you will possibly need for dynamic routing
cause im pretty much repeating everything that is in the docs rn
@gin good job, but i kindly tell u again that u should read the docs link i send above
Welsh TerrierOP
This is where I get confused again. The docs for pages shows HTML in the page.ts file and doesn't mention NOT putting code in there.
page.tsx
the only files that are important for you rn are page.tsx and layout.tsx, everything else that u have in app/, you should move out
this is the first important thing to do, to correctly setup your nextjs app
otherwise it will grow into more issues later
to your root directory
->
components/
app/
fonts/
Welsh TerrierOP
wait
wth
keep app in src
infact everything in src
src/app/
src/components/
public/
Welsh TerrierOP
Next created a 'src' directory from Yarn, but for some reason it doesn't seem to be using it at all. It's only using the /app folder
move everything from app into src/app/
move everything from app/ that is not page.tsx or layout.tsx into src/
Welsh TerrierOP
public is under root dir
good
@gin move everything from app/ that is not page.tsx or layout.tsx into src/
Welsh TerrierOP
This is a little terrifying. like... I'll never get this to run again.... :sweating:
keep this structure ->
src/app/
src/components/
public/
like thata
web is the root folder
Welsh TerrierOP
correct
Welsh TerrierOP
now it looks clean
@Welsh Terrier Click to see attachment
move the components and fonts folder out
into src/
they are currently in src/app/
Welsh TerrierOP
this happens because your fonts folder is now in the parent folder
cant u use @ as the path
@ is the root path in this case
so @/fonts
@gin cant u use @ as the path
Welsh TerrierOP
I don't see any @ s??? what do I need to do?
i mean importing
import your GeistMonoVF.woff from @/fonts/GeistMonoVF.woff
Welsh TerrierOP
this?
do u want me to help out via anydesk?
Answer
ill fix every issue u have
@gin do u want me to help out via anydesk?
Welsh TerrierOP
yes, but be gentle... What is anydesk?
remote desktop
i can basically control your mouse and keyboard
i helped out many people on this server with that
cause its quick and fast
otherwise i dont have that much time solve all your issues
@gin i can basically control your mouse and keyboard
Welsh TerrierOP
How do you connect to me?
if u open anydesk u will see a code
u send me that and i connect to you, and you only give me mouse and keyboard permissions
one sec
shitty anydesk is doing license errors rn
@Welsh Terrier i can connect now
there we go
Welsh TerrierOP
fantastic!! thank you so much!!!
@Welsh Terrier fantastic!! thank you so much!!!
no problem
glad i could help
u can mark this thread as solved
@gin u can mark this thread as solved
Welsh TerrierOP
? how
rightclick on my message where i told u to hop into anydesk
and go to apps -> mark solution
thanks ❤️
@gin @Welsh Terrier glad it works out this time but for the future please don't do this.

we want to avoid dealing with cases where a computer gets compromised after someone grants an internet stranger system access, there are way too many cases like that, you can look them up.

remember here we are all strangers, you should not trust me or anyone else here more than a random dude you see on the street.
you can use zoom or DM (both highly discouraged) if you want to, but please don't grant a stranger system access to your computer