Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12171

How to do redirect to another page in inside the function by "react-router-dom": "^6.21.1"?

$
0
0

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

Viewing all articles
Browse latest Browse all 12171

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>