Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router 4 Regex paths - match doesn't find parameters

I'm trying to have regex pattern matching for react router 4, but unfortunately the this.props.match.params.id is not parsing the path correctly, and shows up as undefined.

Examples of paths i want to be able to access:

  1. /gps
  2. /gps/air
  3. /gps/a0b6dc45-11a1-42c5-afdb-a62822d109dc
  4. /gps/air/a0b6dc45-11a1-42c5-afdb-a62822d109dc

My component:

import React from "react";
import PropTypes from "prop-types";
import { withRouter, } from "react-router";
import { Switch, Route } from "react-router-dom";

class Foo extends React.Component {
    render(){
        console.log(this.props.match);
        return <div>Match: {this.props.match.params.id}</div>
    }
} 

const industryRegex = "(air|motor|ship)";
const guidRegex = "([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})?"; // Note the questionmark ? at the end

const pathId =            `/gps/:id${guidRegex}`;
const pathIndustry =      `/gps/:industryType${industryRegex}`;
const pathIndustryAndId = `/gps/:industryType${industryRegex}/:id${guidRegex}`;

console.log(pathId);
console.log(pathIndustry);
console.log(pathIndustryAndId);

const AppComponent = ({...props}) => {
    return (
        <Switch>
            <Route path={pathId}                     component={Foo} />
            <Route path={pathIndustry}               component={Foo} />
            <Route path={pathIndustryAndId}          component={Foo} />
        </Switch>
    )
};

export default withRouter(AppComponent);

Trying the following path /gps/a0b6dc45-11a1-42c5-afdb-a62822d109dc results in this.props.match.params.id being undefined.

Any help much appreciated

like image 691
Cristian E. Avatar asked Feb 10 '18 17:02

Cristian E.


People also ask

How do you find the parameters on a react router?

Get a single Query String value location.search object: import React from 'react'; import { useSearchParams } from 'react-router-dom'; const Users = () => { const [searchParams] = useSearchParams(); console. log(searchParams); // ▶ URLSearchParams {} return <div>Users</div>; };

How can I ensure a route matches only if the complete path matches?

If the intention is to strictly match only /items , the <Route/> component accepts an exact prop. Adding this ensures that only the pathname that exactly matches the current location is rendered. Below is an example that uses the exact prop.

Why is match undefined react router?

react-router-dom v5 If using RRDv5 if the match prop is undefined this means that BookScreen isn't receiving the route props that are injected when a component is rendered on the Route component's component prop, or the render or children prop functions.

Which props should you use to match exactly the path you have for routing?

pathname will match exact location path.


1 Answers

You're missing a question mark at the end of the industry regex to mark it as option to pass your case 3. Otherwise you regex works for all your stated cases.

The fixed regex should look like this: /gps/:industryType(air|motor|ship)?/:id([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})?

Pro tip: For react router 4, you can test your regex and validate all your use cases here: https://pshrmn.github.io/route-tester/

like image 199
Shayan C Avatar answered Oct 14 '22 02:10

Shayan C