Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  85] [ 2]  / answers: 1 / hits: 5236  / 2 Years ago, fri, july 29, 2022, 12:00:00

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 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 }} />
);
}

More From » reactjs

 Answers
9

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 }} />
);
}

[#48] Tuesday, July 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
efren

Total Points: 186
Total Questions: 85
Total Answers: 112

Location: Turkmenistan
Member since Sat, Apr 16, 2022
2 Years ago
efren questions
;