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:
- /gps
- /gps/air
- /gps/a0b6dc45-11a1-42c5-afdb-a62822d109dc
- /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