I have a React Route that looks something like this...where it conditionally renders a Component based on the value of a URL parameter:
<Route path="/blog/:postId" render={(props) = {
const postId = props.match.params.postId; // extract post from url
return (
post
? <Post {...props} postId={postId} />
: <div>Post does not exist</div>
);
}
I want only authenticated users to be able to access this Route, which means I'd be turning it into a PrivateRoute like this (from the docs):
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
And then I'd use the Private Route like this...
<PrivateRoute path="/blog/:postId" component={Post} />
But how can I examine the URL parameters within the PrivateRoute and conditionally render the Post component depending on the URL parameter?