Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading