Can someone explain why "condition" is not triggering re-render when it is changed when the response is received and state is set?
When this code is run, it always redirects to "/unauthorized/login" even when the response is true. I thought page is always re-rendered when the state is changed.
const PrivateRoute = props => {
const [condition, setCondition] = useState('');
useEffect(() => {
if (!condition) {
getUserAccess();
}
}, [condition]);
const getUserAccess = async () => {
const response = await axios.get("/some-api");
const status = response.data; /* status will either be true or false */
setCondition(status);
}
return condition ? (<Route path={props.path} exact={props.exact} component={props.component}/>) :
(<Redirect to="/unauthorized/login"/>);
};
export default PrivateRoute;
conditionstate updated to a truthy value? What is the value ofstatus?useState()? It must befalseortruefor conditional