2

I am using React Router & I have a nested route which have a query parameter called ?q & my routing code looks like this:

<Route path="/" component={TopNavContainer}>
    <Route path="search?que=:q" component={SearchContainer} />
</Route>

but when I try to access my route as http://localhost:8000?q=machine It only loads the content of the parent container which is TopNavContainer.

Any help is appreciated.

Regards Vaibhav

3
  • 1
    I think no need to specify query param in route configurations. You can easily remove ?que=:q. and you can catch query params inSeachContainer by this.props.location.query or use incomponentWillReceiveProps by nextProps.location.query each time query changed Commented Sep 23, 2016 at 6:25
  • ok I will try that Commented Sep 23, 2016 at 6:34
  • yeah this worked, Thanks! Commented Sep 23, 2016 at 7:45

2 Answers 2

1

It could also be the case that you have forgot to render any child component corresponding to route changes inside parent main component which in your case is TopNavContainer.

import React, { Component } from 'react';

export default class TopNavContainer extends Component {

    render() {
        return (
            <div className="topNavContainer">
                {this.props.children}
            </div>
        );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nope, that's not the case
0

For this route configuration

<Route path="/" component={TopNavContainer}>
    <Route path="search?que=:q" component={SearchContainer} />
</Route>

Your url (http://localhost:8000?q=machine) don't match the path, the correct should be http://localhost:8000/search?que=machine

As a recommendation for SEO, the url could be

<Route path="search/:q" component={SearchContainer} />

that match http://localhost:8000/search/machine

1 Comment

This is a route parameter. The OP is looking for query parameters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.