Conditional display of component based on Route matching

I am looking to conditionally render a component based on the route (using React Router), and the component should return null if it matches any path pre-defined in an array or some sort of similar data structure, where I do not have to be reliant on a <Switch>/<Route> setup. Currently here is what I have but it is clearly inefficient and not robust at all.

const Component = (props) => {
    const path = props.location.pathname;

    const paths_to_hide = ["/path/to/something", "/path/to/A", "/path/to/B"];
    if (paths_to_hide.indexOf(path) != -1) return null;
    return (

For example, if I want to match the following paths:

  1. /path/to/something
  2. /path/to/something/<any path that follows after this>
  3. /path/<random string>/fixed
  4. /newPath/<random string>

Note that this list is not just limited to 4 items, which is why I’m trying to stray away from having inline <Route> matching as I’m looking for a more scalable approach which I can save in a config file and have imported as an array or some similar data structure.

Currently my implementation will only be able to identify the first item, and there is no way to match the subsequent items, using the indexOf() function. What would be the best way to accomplish this? Any help is appreciated, thank you!

182 thoughts on “Conditional display of component based on Route matching”

  1. [url=]generic accutane rx cost[/url] [url=]cost of ivermectin medicine[/url] [url=]tadalafil tablets 20 mg price in india[/url] [url=]sildenafil 25 mg buy[/url] [url=]sildenafil citrate tablets[/url] [url=]augmentin prescription cost[/url] [url=]tadalafil india online[/url] [url=]tadalafil cost in india[/url] [url=]viagra prescription cost canada[/url] [url=]where to get viagra in australia[/url]

  2. [url=]cialis 1000 mg[/url] [url=]plaquenil 400 mg daily[/url] [url=]stromectol 3mg tablets[/url] [url=]buy viagra paypal uk[/url] [url=]generic cialis us[/url] [url=]prednisolone sodium phosphate[/url] [url=]tadalafil india brand[/url] [url=]where can i order real viagra[/url]

  3. [url=]buy modafinil singapore[/url] [url=]accutane otc drug[/url] [url=]buy viagra online legitimate[/url] [url=]script pharmacy[/url]

  4. [url=]lisinopril 10mg tablet[/url] [url=]tadalafil 20 mg mexico[/url] [url=]cheap viagra for sale[/url] [url=]best price tadalafil 20 mg[/url]


Leave a Comment