0

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" }?

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
PLTRNX
  • 11
  • 2
  • I can't reproduce the issue as you describe here in a running [codesandbox](https://codesandbox.io/s/urlsearchparams-not-parsing-query-string-f2z0yb). Could you try forking my sandbox and adding more of your code to see if the issue is elsewhere. Share the link to your sandbox. – Drew Reese May 12 '22 at 15:26

1 Answers1

0

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
Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46
  • Thank you for your answer. It was a misunderstanding on my end, for some reason I thought the `entries()` method returned key/value pair of the params... Note to self; read the web docs more closely next time! – PLTRNX May 12 '22 at 15:50