最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - import error: 'useHistory' is not exported from 'react-router-dom' - Stack Overflow

programmeradmin8浏览0评论

I am trying to import useHistory from 'react-router-dom', however, I get this error: import error: 'useHistory' is not exported from 'react-router-dom'.

I have checked other answers as well such as Attempted import error: 'useHistory' is not exported from 'react-router-dom' this but to no avail. My package.json looks like this

I am using useHistory as such,

import React from 'react';
import { useState, useEffect } from 'react';
import { useHistory } from 'react-router-dom';

export default function Login(){
    const history = useHistory();
    console.log(history)
}

However, this line works fine and does not cause import issues from react-router-dom.

import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'

Does anyone have an idea how to solve this issue? much appreciated.

I am trying to import useHistory from 'react-router-dom', however, I get this error: import error: 'useHistory' is not exported from 'react-router-dom'.

I have checked other answers as well such as Attempted import error: 'useHistory' is not exported from 'react-router-dom' this but to no avail. My package.json looks like this

I am using useHistory as such,

import React from 'react';
import { useState, useEffect } from 'react';
import { useHistory } from 'react-router-dom';

export default function Login(){
    const history = useHistory();
    console.log(history)
}

However, this line works fine and does not cause import issues from react-router-dom.

import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'

Does anyone have an idea how to solve this issue? much appreciated.

Share Improve this question edited Sep 19, 2021 at 19:10 TanDev asked Sep 19, 2021 at 2:05 TanDevTanDev 4373 gold badges8 silver badges15 bronze badges 3
  • Is your Login ponent's route wrapped inside BrowserRouter? – Sanket Shah Commented Sep 19, 2021 at 2:13
  • Yes it actually is. – TanDev Commented Sep 19, 2021 at 18:27
  • why are you reassigning window.history? – Nikos Commented Aug 3, 2022 at 7:33
Add a ment  | 

4 Answers 4

Reset to default 14

UseHistory have already been replaced with useNavigate So try this

发布评论

评论列表(0)

  1. 暂无评论