need help in Dynamic Routing
Answered
Cape lion posted this in #help-forum
Cape lionOP
Guy I came across a scenario where i need project id that is dynamic , For management/123 that take me to dashboard of that project that shows stats about the project, well the problem is i have to show feature and issues pages as well that are another routes same for create feature and create issue
Is there any way we can do something like this
/management/123/issues
/management/123/features etc
But when i place features folder inside management/[id]/page.tsx and feature issues folder it show you cant you other routes with in dynamic route also document says
Dynamic routes must have page.tsx file not any other routes
Whats the best solution for my problem or anyway to get this done
All i need is project id , when i am at features pages as well as feature detail page or create feature page as the user the interacts to make some changes i have to use project id to make change in backend
If you need more details i am happy to provide
Is there any way we can do something like this
/management/123/issues
/management/123/features etc
But when i place features folder inside management/[id]/page.tsx and feature issues folder it show you cant you other routes with in dynamic route also document says
Dynamic routes must have page.tsx file not any other routes
Whats the best solution for my problem or anyway to get this done
All i need is project id , when i am at features pages as well as feature detail page or create feature page as the user the interacts to make some changes i have to use project id to make change in backend
If you need more details i am happy to provide
Answered by B33fb0n3
yeah then get them xD
I thought you know how to get the fragments? https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#example
I thought you know how to get the fragments? https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#example
52 Replies
Cape lionOP
I have tried that i put both folder /features and /issues inside the [id] it throws and error says you can't use it like tbis
Should i create a one folder inside [id] and put my Features and Issues folder within that folder solve my issues?
Route would be like this
/management/123/project-detail/features
/management/123/project-detail/issues
/management/123/project-detail/features
/management/123/project-detail/issues
Cape lionOP
@B33fb0n3
for me it's working like that. I think it should work for you as well like that ^^
Cape lionOP
Well i'll try it out
@Cape lion tried? 🤔
Cape lionOP
Yup it works , But now the issue coming in navigation , I am unable to keep pathname as it is without using 'use client' i have to make layout or page a client component
That leading me to lose server components powers
const pathName = usePathname()
const projectID = pathName.split('/')[2]
const issuesPath = `/management/${projectID}/project-detail/issues`
const featuresPath = `/management/${projectID}/project-detail/features`
const projectPath = `/management/${projectID}`
const GeneralLinks = [
{ name: 'Dashboard', icon: <BiHome className='text-xl mx-2' />, href: '/dashboard' },
{ name: 'Project Statistics', icon: <BiHome className='text-xl mx-2' />, href: `${projectPath}` },
];
const WorkspaceLinks = [
{ name: 'Issues', icon: <AiOutlineIssuesClose className='text-xl mx-2' />, href: `${issuesPath}` },
{ name: 'Features', icon: <GrTask className='text-xl mx-2' />, href: `${featuresPath}` },
{ name: 'Chat', icon: <HiOutlineChatAlt2 className='text-xl mx-2' />, href: '#' }
];why do you NEED to have a client comp?
Cape lionOP
Look at the example its my sidebar
My pageoption component that is reused in different layouts
'use client'
import React from 'react';
import { usePathname } from "next/navigation"
import Link from 'next/link';
interface SidebarNavProps extends React.HTMLAttributes<HTMLElement> {
items: {
href: string
title: string
}[]
}
export default function PageOption({ items }: SidebarNavProps) {
const pathname = usePathname()
return (
<nav className={"flex py-4 flex-wrap lg:justify-start justify-around w-full lg:w-3/12 space-x-2 px-2 lg:flex-col lg:space-x-0 lg:space-y-3"}>
{items.map((item) => (
<Link
key={item.href}
href={item.href}
className={`${ pathname === item.href ? "bg-[#dfeefe] " : "hover:bg-transparent hover:underline"} justify-start w-max text-xs sm:text-base rounded px-6 py-2 `}
>
{item.title}
</Link>
))}
</nav>
);
}Utilising the page option now if i don't make this layout as client component i am unable to use usepathname hook
import { PageHeading, PageOptions } from '@/components/dashboard';
import React from 'react'
import { Toaster } from 'sonner'
const options = [
{ title: 'Features', href: '/management/features' },
{ title: 'Create Feature', href: '/management/features/create-feature' },
];
export default function FeatureLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<div className='w-full h-full px-4 flex flex-col'>
<PageHeading title='Feature Management' subtitle='' />
<div className='py-4 flex flex-col lg:flex-row flex-grow overflow-y-auto'>
<PageOptions items={options} />
<div className='overflow-y-auto w-full border-l h-full'>{children}</div>
</div>
<Toaster richColors position="bottom-right" />
</div>
)
}@B33fb0n3 let me know if you need any more info
Can you isolate your problem and share it on an online code editor?
@Cape lion tried? 🤔
Cape lionOP
Hey @B33fb0n3 i tried but it look like there is no possible way to do to get pathname in Server component
Why do you need the pathname? You should only need the dynamic fragments?
Cape lionOP
Yes i need the dynamic Segment yes !
yeah then get them xD
I thought you know how to get the fragments? https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#example
I thought you know how to get the fragments? https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#example
Answer
@Cape lion got them? 🤔
@B33fb0n3 yeah then get them xD
I thought you know how to get the fragments? https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#example
Cape lionOP
haha I do know about these bro , actually in their official docs they said pathname is not available for server components , anyways i tradeoff this matter with something else i implement all routes in sidebar
Reading the current URL from a Server Component is not supported. This design is intentional to support layout state being preserved across page navigations.
Compatibility mode:
usePathname can return null when a fallback route is being rendered or when a pages directory page has been automatically statically optimized by Next.js and the router is not ready.
Next.js will automatically update your types if it detects both an app and pages directory in your project.
Compatibility mode:
usePathname can return null when a fallback route is being rendered or when a pages directory page has been automatically statically optimized by Next.js and the router is not ready.
Next.js will automatically update your types if it detects both an app and pages directory in your project.
@Cape lion haha I do know about these bro , actually in their official docs they said pathname is not available for server components , anyways i tradeoff this matter with something else i implement all routes in sidebar
yes, the pathname is not available for server components. But the dynamics parts are available. So you can easily get the dynamic segments (the values) and do stuff with them ^^
Cape lionOP
/management/1bdjs77shejsi837272/features
Now in server component i want this route how can i get it? In layout as params or search params arent available either
Now in server component i want this route how can i get it? In layout as params or search params arent available either
as I said:
So you can access the dynamics parts via your props
the pathname is not available for server components, but the dynamics parts are available.
So you can access the dynamics parts via your props
@Cape lion /management/1bdjs77shejsi837272/features
Now in server component i want this route how can i get it? In layout as params or search params arent available either
West African Lion
U mean to get segment object data?
@West African Lion U mean to get segment object data?
he mean this:
/management/1bdjs77shejsi837272/features
1bdjs77shejsi837272
/management/1bdjs77shejsi837272/features
1bdjs77shejsi837272
@B33fb0n3 he mean this:
||/management/||1bdjs77shejsi837272||/features||
1bdjs77shejsi837272
West African Lion
Get it, right?
I'll see
yea. He can easily access them via the props
@B33fb0n3 as I said:
> the pathname is not available for server components, but the dynamics parts are available.
So you can access the dynamics parts via your props
Cape lionOP
We can't pass any props to layout isn't it?
West African Lion
I'm using nextjs v14 for api
So I found this like to get segment object data https://nextjs-forum.com/post/1174081937867014226#message-1174085233759490058
It's works for me to get params
@Cape lion We can't pass any props to layout isn't it?
with dynamic segments, you can
@West African Lion So I found this like to get segment object data https://discord.com/channels/752553802359505017/1174081937867014226/1174085233759490058
please NOT! That's a completly different topic!
Thats about
Thats about
route handlers@B33fb0n3 please NOT! That's a completly different topic!
Thats about route handlers
West African Lion
Oh, alr mb
@Cape lion
Dynamic Segments are passed as the params prop to layout, page, route, and generateMetadata functions.
@B33fb0n3 with dynamic segments, you can
Cape lionOP
Damn !!!! It man that what i am missing
Hell yeah i am reading the docs again with 4 eyes + zoom in
@B33fb0n3 well, it's one of the first sentences on the page lol
Cape lionOP
That where i didn't focus lil
oh ok, happy to help ^^
would you be able to mark this message as solution?
https://nextjs-forum.com/post/1177270919111127100#message-1177981688752652419
https://nextjs-forum.com/post/1177270919111127100#message-1177981688752652419
@B33fb0n3 oh ok, happy to help ^^
Cape lionOP
Thanks alot mate , thank you so much !
your welcome 🙂
Good job! See you soon ^^