I have an issue with redirect to another page. I can't use useNavigate() inside the function. Because it's not an component. redirect also not working. How can i redirect?
import { redirect, useNavigate } from "react-router-dom";import { ILogin } from "../Pages/Login/ILogin";import { decode } from "./encodeDecode";export const parseToken = () => { const nav = useNavigate(); try { const token: ILogin = decode(localStorage.getItem("token")); return token; } catch (error) { console.log(error); alert("Invalid Credentials"); localStorage.clear(); redirect("/login"); nav("/login"); }};
when call redirect it's not workingwhen call nav it's throw an error as
Line 6:15: React Hook "useNavigate" is called in function "parseToken" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooksSearch for the keywords to learn more about each error.webpack compiled with 1 error and 1 warning