Home > Blockchain >  How to capture search params in react when I search with form action="/search"?
How to capture search params in react when I search with form action="/search"?

Time:06-13

I have a form which puts to URL "/search/?q=my searching query..". How to get this my searching query.. as params? This route is considered..

This is my form..

This what I get in my URLenter image description here

How to get the my "searching query.." as params? When I try by useParams it doesnt work because of "?" symbol.

Of course I can do it without that action="/search/" on my input but I see it more true way to do searching.

CodePudding user response:

You've defined ":query" as url parameter, but "?q=my searching query.." is a search query. You could get it like:

const { search } = useLocation();
const searchParams = new URLSearchParams(search);
const searchQuery = searchParams.get('q')

And you could also remove the ":query" from route path.

  • Related