I have these routes inside my App.js file:
<LoggedInRoute userData={userData} path="/code/:id/view">
<ViewCode />
</LoggedInRoute>
<LoggedInRoute userData={userData} path="/code/feed/:userId">
<CodeFeed />
</LoggedInRoute>
// userData -> state representing user session info
Here it is my LoggedInRoute component:
import React from 'react';
import { Redirect, Route } from "react-router-dom";
function LoggedInRoute(props) {
return (
<Route
render={({ location }) =>
props.userData.id ? (
props.children
) : (
<Redirect
to={{
pathname: "/login",
state: { from: location }
}}
/>
)
}
/>
);
}
export default LoggedInRoute;
I am using the useParams() function (from react-router-dom) to extra id and userId in my child components ViewCode and CodeFeed. now the problem is that are extracted as undefined values. If I use Route instead of LoggedInRoute, everything works fine. It seems like my LoggedInRoute component doesn't pass the path to the child components. I checked the location object, from useLocation(), and the path it's the right one in the child components. What I am missing? Thanks in advance, guys :)