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

URLSearchParams not parsing query string

Using react-router-dom version 5.

Some component, rendered when matching a <Route />:

...

const { search } = useLocation();
const params = new URLSearchParams(search);

  useEffect(() => {
    console.log(search); // "?paramOne=1&paramTwo=2"
    console.log(params); // {}
  }, []);

...

Why does params not show { paramOne: "1", paramTwo: "2" }?

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

>Solution :

You are not using URLSearchParams as you should. You are getting URLSearchParams object and if you want to get it as a string, you should log params.toString()

Check out these links:

  1. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams
  2. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString
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