How can I get request search parameters in a App router component?
Unanswered
American black bear posted this in #help-forum
American black bearOP
I used App router in my projects, but I am confusion that how can I get the search parameters of request in a server component?
I tried to use server action, however I did not have a solution to get the parameters.
Could you do me a favour to guide me, Thanks !!!
I tried to use server action, however I did not have a solution to get the parameters.
Could you do me a favour to guide me, Thanks !!!
9 Replies
Oh I struggled with that for a while but its actually pretty dang simple. So there is one parameter that will be in a route method. Lets use GET so that you can be sure you're not working with POST material.
const GET = async (request) => {
const params = request.nextUrl.searchParams
const name = searchParams.get('name')Sorry I was using page routing at first when I first sent that
@ASittingDuck Oh I struggled with that for a while but its actually pretty dang simple. So there is one parameter that will be in a route method. Lets use GET so that you can be sure you're not working with POST material.
js
const GET = async (request) => {
const params = request.nextUrl.searchParams
const name = searchParams.get('name')
American black bearOP
Thank you for your reply, I am a newbie for Next.js
In fact, I tried use route handler, however, I don't know the relatioship between route handle and server component so that I don't konw how to pass the data to server component.
In fact, I tried use route handler, however, I don't know the relatioship between route handle and server component so that I don't konw how to pass the data to server component.
So you are talking about API routes right?
American black bearOP
I have a server component, I want to implement a feature that I can get the search parameters in server component and by using the parameter for fetch the data from api server.
I just used App router.
I just used App router.
So you are talking essentially about a search bar that talks to your apps internal API and returns a result to the client?
@ASittingDuck So you are talking essentially about a search bar that talks to your apps internal API and returns a result to the client?
American black bearOP
//route: http://domain.com/test?id=1000;
//I want to make the ServerComponent render in server side
const ServerComponent = async ()=> {
//here
const res = await (()=> {
"use server";
return fetchData('url', {id})
// I want to get the id in the route here, but I don't know how to get it in server component
})();
return #Unknown Channel</>
}
I want to implement the feature like above pseudo code. Thank you for your guide again!!!
//I want to make the ServerComponent render in server side
const ServerComponent = async ()=> {
//here
const res = await (()=> {
"use server";
return fetchData('url', {id})
// I want to get the id in the route here, but I don't know how to get it in server component
})();
return #Unknown Channel</>
}
I want to implement the feature like above pseudo code. Thank you for your guide again!!!
You could make use of prop drilling and pass the
For example:
id from the page component to your server component. Assuming that your page is for a dynamic route.For example:
export default function Page({ params }: { params: { id: string } }) {
return <ServerComponent id={id} />
}