await params in v15
Unanswered
Giant panda posted this in #help-forum
Giant pandaOP
I'm trying to access the params.slug array from a Server Component. The documentation says that those now are asynchronous and I should await for them to use them. BUT I'm always getting undefined.
I have a dynamic catch-all route [[...slug]]/page.tsx. Inside is my async function
Does anyone have an idea of what's wrong with this simple code?
I have a dynamic catch-all route [[...slug]]/page.tsx. Inside is my async function
export default async function Home({params}: {params: {slug: string[]}}) {
const {slug} = await params;
console.log(slug, "<<<"); // <<<<< THIS LINE IS ALWAYS RETUNGING UNDEFINED
const {data} = await fetchApolloData(getPathData, {
thepath:
/${slug?.join('/') || ''}});
const initialData = data?.getPathData;
...
Does anyone have an idea of what's wrong with this simple code?
5 Replies
Are you on /? if so, it is undefined, as expected. If you are on /hello it will be [“hello”], if you are on /hello/world it will be [“hello”, “world”].
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments
also your typing is wrong, but that’s not the bug here.
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments
also your typing is wrong, but that’s not the bug here.
Giant pandaOP
hmmm but even If I'm in / when I console log await params will it be undefined? is that the expected behavior?
In v14 when in / and console log params it will log an empty object {}
In v14 when in / and console log params it will log an empty object {}
Asian black bear
That's the same behavior, something like
slug
being undefined
means the entire params
object is just empty.The link joulev posted clearly explains what the expected values are in certain cases.
it is an empty object for me