Query Strings

When you're building for the web, sometimes you need to pass information via the URL. To do this, you can use a query string.

You've probably seen them before IRL. Here's an example from Twitter's analytics page.

image

Now the thing is, how do we get those query strings from the url and into our component? React router gives us a hook that allows us to easily do that called useLocation.

or we could do:


But as you can see, that just gives us the actual string, not the params. You’d expect they’d provide it as an object that you can then destructure them from or something, but no, maybe on the next react-router version. For now, we have 2 ways of breaking this up:


Natively (not supported by IE)


NPM

Copyright 2023 © Borja Leiva

Made within London