I have a PrivateRoute
component,
interface Props {
path: string,
exact: boolean,
component: React.FC<any>;
}
const PrivateRoute: React.FC<Props> = ({ component, path, exact }) => {
return (
<Route
path={path}
exact={exact}
>
{
getCookie('name').length !== 0 ? component : <Redirect to="/login" />
}
</Route>
);
}
But when I try to use useState
hook inside a PrivateRoute
, I get the invalid hook call
error.
Example,
const [some, setSome] = useState(true);
// Calling this inside a private route will throw an error!
How to solve this?
Edit
This is how PrivateRoute
is used,
<Switch> // from react-router-dom
<Route path="/login" component={Login} />
<PrivateRoute path="/" exact component={Home} />
</Switch>