In this post, we will explore How To Fix - Attempted Import Error: 'useHistory' is Not Exported from 'React-Router-Dom'. Error Logs -
Attempted import error: 'useHistory' is not exported from 'react-router-dom'
This error occurred during the build time and cannot be dismissed
import error: 'useHistory' is not exported from 'react-router-dom'.
export 'useHistory' (imported as 'useHistory') was not found
export 'useHistory' was not found in 'react-router-dom'
TypeError: Cannot read property 'push' of undefined
Ideally you should be using the latest React-Router-Dom. Do the below sanity checks first.
npm view react version
npm list --depth=0
OR
Go to project-path ---> package.json File
Check for "react-router" value in the File .
import { useNavigate } from 'react-router-dom';
const history = useHistory();
Replace with
const navigate = useNavigate();
history.push('/path')
Replace with
navigate('/path')
history.replace('/path')
Replace with
navigate('/path', { replace: true })
To Use State
navigate('/path', { state: { name:'SOME\_NAME' }})
To Summarize
// VERSION 5 Code
import { useHistory } from "react-router-dom";
let history = useHistory();
history.push("/home");
// VERSION 6 Code
import { useNavigate } from "react-router-dom";
let navigate = useNavigate();
navigate("/home");
Hope this helps to fix the issue.
Attempted import error: 'useHistory' is not exported from 'react-router-dom' , ,This error occurred during the build time and cannot be dismissed ,import error: 'useHistory' is not exported from 'react-router-dom'. ,export 'useHistory' (imported as 'useHistory') was not found ,export 'useHistory' was not found in 'react-router-dom' ,TypeError: Cannot read property 'push' of undefined , ,attempted import error: 'usenavigate' is not exported from 'react-router-dom'. ,attempted import error: 'switch' is not exported from 'react-router-dom'. ,react-router-dom usehistory ,usehistory in react router-dom v6 ,attempted import error react-router does not contain a default export imported as 'switch ,usenavigate vs usehistory ,usenavigate() may be used only in the context of a <router> component. ,usenavigate not working ,usehistory' is not exported from 'react-router-dom' ,routes' is not exported from 'react-router-dom' ,cannot get route react router ,react-router-dom usehistory not working ,import link from 'react-router-dom' not working , ,switch' is not exported from 'react-router-dom ,usehistory in react router-dom v6 ,withrouter' is not exported from 'react-router-dom ,react router-dom v6 ,usehistory react-router-dom ,usenavigate vs usehistory ,usenavigate react router-dom v6 ,usenavigate react router-dom