Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error when try to use React Router v6: Attempted import error: 'Action' is not exported from 'history'

I have created a new react application using

npx create-react-app app-name

Then I changed my directory to that app folder and then ran the following command.

npm install react-router@next react-router-dom@next

Then I ran my application and it worked fine. Then I changed the default code and used Routes, Route, etc. Here is APP code

import React from 'react';
import {BrowserRouter as Router, Routes, Route} from "react-router-dom"

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Home />} />
      </Routes>
    </Router>
  );
}

function Home()
{
  return <div>
        <h1>Home</h1>
    </div>
}

export default App;

I get the error:

Attempted import error: 'Action' is not exported from 'history'.

Path of the error is:

./node_modules/react-router/index.js

Any Idea what is wrong with it?

like image 552
Muhammad Umar Gulzar Avatar asked Jul 24 '20 10:07

Muhammad Umar Gulzar


People also ask

How do I fix attempted import error in react JS?

The React. js "Attempted import error 'X' is not exported from" occurs when we try to import a named import that is not present in the specified file. To solve the error, make sure the module has a named export and you aren't mixing up named and default exports and imports.

How do I use a Router with React v6?

React Router v6 makes it dead easy to create React apps with complex UIs that has routing between different portions, you can simply declare a Router component such as BrowserRouter or HashRouter and put,inside of that, a bunch of child Routes components that has props which indicate the path to be matched and the ...

Why can’t I import switch from react-router-Dom V6 (Version 6)?

When we try to import Switch from the react-router-dom v6 (Version 6) package, we’ll get the above error. Let’s have a look at what we can do about it. The Switch has been replaced with Routes in react-router-dom v6. Additionally, you must alter the Route declaration.

Why ‘routes’ is not exported from ‘react-router-Dom’?

This can be useful to fix the issue ” ‘Routes’ is not exported from ‘react-router-dom’ “, which can be caused because you are using older version of react-router-dom but newer version of code.

What happened to usehistory in react-router-Dom V6?

In react-router-dom v6 useHistory () is replaced by useNavigate (). Show activity on this post. Change history.replace ('/path') with navigate ('/path', { replace: true }) Want to use state in push/navigate do navigate ('/path', { state: { name:'Xyz' }})

Why is my action not being exported from history?

The actual issue is that the version of history is most likely old and needs to be updated. As other answers have stated, the solution here is to install history@^5.0.0. However, if you're using TypeScript you might still run into the 'Action' is not exported from 'history' error.


Video Answer


1 Answers

You need to install another package called history. Do npm i history.

More info here

like image 144
kcsujeet Avatar answered Sep 19 '22 09:09

kcsujeet