We used our own in-house Route-component to track routes in react-router-dom v5. It looked roughly like this:
import { Switch, Route } from 'react-router-dom';
// EDIT: This was React-Router v5 code, in v6 <Switch> is replaced by <Routes>
// and <Route component={}> is replaced by <Route element={}>
const routes = () => (
<Switch>
<TrackedRoute title="title to track" path="/path/home" component={Home} />
<TrackedRoute title="title to track" path="/path/contact" component={Contact} />
</Switch>
)
const TrackedRoute = ({ title, ...others }) => {
doTrackingThings(title);
return (
<Route {...others} />
);
}
But in React Router v6 the Routes component only accepts its own <Route> objects. This is enforced through type equality. Additionally optional params are no longer allowed in route paths, which made our tracking more complicated. If we want to track which route the user is on, how do we do this in React Router v6?