Next.js Discord

Discord Forum

What do you think that is the best way to get the search params?

Answered
Fenriuz posted this in #help-forum
Open in Discord
Avatar
I can get the search params from my page.tsx using props and send as a prop to my client component. But the client component can get the searchParams using useSearchParams. But what's the best way and why?
Answered by Fenriuz
Ok, thanks for the support. Based in the answers I'll use props.searchParams from my page.tsx because I just need to share the params with one client component, so it's unnecessary use useSearchParams. The hook could be better if I am in a very nested component or I can't access to props.searchParams from my page.tsx. Thank you:)
View full answer

13 Replies

Avatar
if you want to get search params you should be able to just use props.searchParams (only in page.tsx), if this doesn't work then useSearchParams is the next best option which is still good, and fine for production
Avatar
But if I can use both, which one should be my default one?
Avatar
it depends
they both work
I would probably use props.searchParams
because you don't have to use a hook which can cause unexpected behaviour
but then if you only need the searchParams in an inner component (not page.tsx), then you'd have to use useSearchParams
and props.searchParams will work in a server component
Avatar
If you don't need to pass search params to more than 2-3 nested components, then props would be best option
Avatar
unlike useSearchParams because it's a hook
@Fenriuz If I had to choose one, I would choose props, because it works in a server component, but if I need to pass it down multiple components, then I would just useSearchParams
Avatar
Ok, thanks for the support. Based in the answers I'll use props.searchParams from my page.tsx because I just need to share the params with one client component, so it's unnecessary use useSearchParams. The hook could be better if I am in a very nested component or I can't access to props.searchParams from my page.tsx. Thank you:)
Answer
Avatar
Make sure to mark as answer