This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Router: Querystring Parameters

2:22 React lesson by

In this lesson we'll extract props for our component from a querystring in the route url.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson we'll extract props for our component from a querystring in the route url.

Avatar
Dean

informative!! I do have a question ---
How can you use react-router with redux that uses "different" stores, and thus surrounded by different "". I can't figure out how to accomplish this.

In reply to egghead.io
Avatar
Richard

Great job on the set of lectures. I just want to know is it possible to access the Link functionality for form and input, by that I mean can one pass the query from a form input field and have it change the url rather than have a button to click.

React router allows us to pass in query strings as props to any component that's going to be rendered at a specific route, so here I'm creating a quick component called "page." It's simply going to return a div with an h1. To get at a query string we're going to use props.location, then Query, and then the property that we're looking for on the query. The message will be the query parameter in the URL.

If we don't have that, I'm just going to say "hello." We're going to set up our router in our app component. Here's our router. Using the #history, we're just going to set up a single route with a path to forward slash and then the component that'll be rendered there is page.

You can see here we're not passing in any route parameters. We're only going to be using query parameters. I'm going to save that and we've got our hello. Here in the URL right before the _k parameter, I'm going to create a parameter called "message," I'm going to say equals below there and then we'll split that up with an &.

We get our "hello" there from our query string. We'll go ahead and jump back to our route and now we're going to take a look at how to use the query parameters in a link component. I'm going to create a new function here, component here called "links." We're simply going to return a nav with a link component in here.

Rather than using a string in the "to" parameter or property, we're going to pass in an object and we're going to have a key of path name. This is going to represent what our string used to represent. There's our forward slash. Now we're going to pass in a query -- that's another object -- with the key that we want to send along with the query string, and then the value.

Here I'm just going to say go and then we're going to drop our links component right here inside of our page component. I forgot to put a little bit of text there, so we'll say "yo."

When we click on this, our query string with the parameter of "message" and a value of "yo" is passed into our component.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?