Pass custom data to PrivateRoute component in React
You need to pass on the ...rest
params that you pass to PrivateRoute
on to the component, however not all of them need to react the component. You can destructure other params that are needed by React router
const PrivateRoute = ({ component: Component, exact, strict, path, ...rest }) => (
<Route
exact={exact}
strict={strict}
path={path}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} {...rest} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);