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?
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.
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 ...
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.
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.
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' }})
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.
You need to install another package called history
. Do npm i history
.
More info here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With