javascript - Attempted import error: 'Navigate' is not exported from 'react-router-dom' - Stack

pls help me to deal with this error Attempted import error: 'Navigate' is not exported from &

pls help me to deal with this error Attempted import error: 'Navigate' is not exported from 'react-router-dom' my react-router-dom version is 4.1.1 pls don't tell me how update to 5v or v6 because in that verisons i am facing many errors so pls tell how to fix this error in 4.1.1 verison of react-router-dom the error is Attempted import error: 'Navigate' is not exported from 'react-router-dom'. you all can also see the img of the error

my code here

import {  Navigate, useLocation } from "react-router-dom";
import { useAppSelector } from "../app/hooks";

export function PrivateRoute({ children }: { children: JSX.Element }) {
  const { isAuthenticated } = useAppSelector((state) => state.auth);
  const location = useLocation();
  return isAuthenticated ? (
    children
  ) : (
    <Navigate to="/login" state={{ from: location }} />
  );
}

pls help me to deal with this error Attempted import error: 'Navigate' is not exported from 'react-router-dom' my react-router-dom version is 4.1.1 pls don't tell me how update to 5v or v6 because in that verisons i am facing many errors so pls tell how to fix this error in 4.1.1 verison of react-router-dom the error is Attempted import error: 'Navigate' is not exported from 'react-router-dom'. you all can also see the img of the error

my code here

import {  Navigate, useLocation } from "react-router-dom";
import { useAppSelector } from "../app/hooks";

export function PrivateRoute({ children }: { children: JSX.Element }) {
  const { isAuthenticated } = useAppSelector((state) => state.auth);
  const location = useLocation();
  return isAuthenticated ? (
    children
  ) : (
    <Navigate to="/login" state={{ from: location }} />
  );
}
Share Improve this question edited Jul 29, 2022 at 16:08 asked Jul 29, 2022 at 15:54 user20659824user20659824
Add a ment  | 

3 Answers 3

Reset to default 3

Use Redirect instead of Navigate in version 4, like so

import {  Redirect, useLocation } from "react-router-dom";
import { useAppSelector } from "../app/hooks";

export function PrivateRoute({ children }: { children: JSX.Element }) {
const { isAuthenticated } = useAppSelector((state) => state.auth);
const location = useLocation();
    return isAuthenticated ? (
       children
    ) : (
      <Redirect to="/login" state={{ from: location }} />
    );
  }

Recent update to answer:

In version 6 of React Router, the Redirect ponent is replaced with Navigate.

So your code should work now.

Try replacing Navigate with Link.

import { Link, useLocation } from "react-router-dom";
...
<Link to "/login" state={{ from: location }} />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419536a4440402.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信