The docs state
All three render methods will be passed the same three route props match location history
And they showcase it with this example
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Route } from "react-router-dom";
// All route props (match, location and history) are available to User
function User(props) {
return <h1>Hello {props.match.params.username}!</h1>;
}
ReactDOM.render(
<Router>
<Route path="/user/:username" component={User} />
</Router>,
node
);
Seems easy enough.
But then I try it with my code and that's not working.
import * as React from 'react';
import { Switch, Route, BrowserRouter, Redirect } from 'react-router-dom';
import { isFalsy, readFromStorage } from 'utils/general.utils';
import { Homepage } from './pages/homepage/Loadable';
import { Header } from './global/components';
import { Register } from './pages/auth/register/Loadable';
import { Login } from './pages/auth/login/Loadable';
import { ACCESS_TOKEN } from 'constants/storage.constants';
const PrivateRoute = ({ children, ...rest }) => {
const accessToken = readFromStorage(ACCESS_TOKEN);
return (
<Route
{...rest}
render={({ location }) =>
!isFalsy(accessToken) ? (
children
) : (
<Redirect
to={{
pathname: '/login',
state: { from: location },
}}
/>
)
}
/>
);
};
export function App(props) {
const routes = [
{
path: ['/'],
component: Homepage,
exact: true,
private: false,
},
{
path: ['/register'],
component: Register,
private: false,
},
{
path: ['/login'],
component: Login,
private: false,
},
];
return (
<BrowserRouter>
<Header />
<Switch>
{routes.map(route => {
const Component = route.component;
const RouteWrapper = route.private ? PrivateRoute : Route;
return (
<RouteWrapper
key={route.path[0]}
path={route.path}
exact={route.exact}
>
<Component {...props} />
</RouteWrapper>
);
})}
</Switch>
</BrowserRouter>
);
}
And yes I am wrapping my App in ReactDom likeso
ReactDOM.render(
<Provider store={store}>
<HelmetProvider>
<React.StrictMode>
<App />
</React.StrictMode>
</HelmetProvider>
</Provider>,
ROOT,
);
But the Register, Login and Home component don't have the props of the route. What am I doing wrong here?
I am using v5.2.0
