import React from "react";
import { Redirect, Route } from "react-router-dom";
import { useState, useEffect } from "react";
import To_Do_Lists from "./todo";
const ProtectedRoute = ({ component: Component, ...restOfProps }) => {
  const [isValidated, setIsValidated] = useState(false);
  let token = localStorage.getItem("Authorization");
  let config = {
    headers: {
      authorization: token,
    },
  };
  const checkAuth = async () => {
    await axios
      .get("http://localhost:3001/checkAuth", config)
      .then((res) => {
        if (res.status === 201) {
          console.log("res received", res);
          setIsValidated(true);
        }
        console.log(1, isValidated);
      })
      .catch((err) => console.log(err));
    console.log(2, isValidated);
  };
  useEffect(() => {
    checkAuth();
  }, []);
  return (
    <Route
      {...restOfProps}
      render={(props) =>
        isValidated ? <Component {...props} /> : <Redirect to="/" />
      }
    />
  );
};
export default ProtectedRoute;
When this component is used within react-router-dom, after i log in on the home page, it successfully makes a GET request. It even logs the "res received" inside the conditional IF statement. But it does not update the state variable via setIsValidated(true). Both console.log's (both 1 and 2) of the state variable, show it stays false. What am I missing here?
 
     
    