报错:'Action'未从'history'导出,未导入Action却出现该问题求助
Let's break down what's going on here and how to fix this frustrating error:
Why This Error Occurs
The 'Action' is not exported from 'history' issue stems from a version mismatch between react-router-dom and the history package you installed after rolling back.
Action is a type introduced in newer versions of the history library—it's used to describe navigation actions like POP, PUSH, or REPLACE. When you downgraded history to an older version, that version doesn't include the Action export, but your current react-router-dom version relies on it to function properly.
Step-by-Step Solutions
1. Install a history version compatible with your react-router-dom
First, check your package.json to confirm which version of react-router-dom you're using:
- If you're on
react-router-dom@5.x, you needhistory@4.x(e.g.,history@4.10.1is a reliable compatible version) - If you're on
react-router-dom@6.x, you needhistory@5.x
Run these commands to align the versions:
# Uninstall the mismatched history version npm uninstall history # Install the compatible version (replace with the one matching your react-router-dom) npm install history@4.10.1
2. Let your package manager handle dependencies (recommended)
Skip manually specifying the history version entirely—remove it from your package.json, then run:
npm install
This will let npm automatically install the exact history version that your react-router-dom depends on, eliminating version conflicts for good.
Quick Code Improvement
Looking at your App component, you're using the base Router from react-router-dom. In most cases, you should use BrowserRouter instead (it handles the browser's history API automatically). Swap it out like this:
// Replace this import: import { Router, Switch, Route } from 'react-router-dom'; // With this: import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
This avoids needing to manually create and pass a history instance, which can prevent additional version-related headaches.
内容的提问来源于stack exchange,提问作者Salil Rajkarnikar

