Next js search params gives undefined
Answered
Harrier posted this in #help-forum
HarrierOP
When using server component, I am getting the search params ( passed in props ) as undefined.
Thanks in advance!
Thanks in advance!
Answered by chisto
yeah, it gotta be used on the main Page and pass it to the components
7 Replies
you can use the hook useSearchParams() to get the searchparams inside the children and no need to pass it thru props
HarrierOP
Hey @chisto Thanks for replying. So to use the useSearchParams hook I have to make this component a client component. I think search params are automatically available as props in a page special file.
I think I was getting them as undefined because I used it in a separate component.
I think I was getting them as undefined because I used it in a separate component.
So I confirmed it, the issue was I was using the searchParams prop directly in my component instead of page.tsx
yeah, it gotta be used on the main Page and pass it to the components
Answer
@Harrier searchParams will be only available in the
Check out this doc
https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional
page.tsx
Check out this doc
https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional
You can pass it to the child component as a prop or make your component a client component and use hook (https://nextjs.org/docs/app/api-reference/functions/use-search-params) to get search params
@Harrier Solved?