export 'redirect' (imported as 'redirect') was not found in 'react-router-dom'
时间: 2023-04-23 15:03:57 浏览: 1710
"export 'redirect' (imported as 'redirect') was not found in 'react-router-dom'" 的意思是在 'react-router-dom' 中找不到名为 'redirect' 的导出项。
可能是因为你在代码中使用了 'redirect',但是没有正确导入它。你需要检查你的代码,确保你已经正确地导入了 'redirect'。
如果你仍然无法解决问题,你可以尝试更新你的 'react-router-dom' 版本或者查看官方文档以获取更多帮助。
相关问题
export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'
这个错误通常是因为你使用了错误的导入方式。在 React Router v6 中,Switch 被移动到了 react-router 包中,所以你需要从 react-router 中导入 Switch,而不是从 react-router-dom 中导入。可以尝试使用如下代码导入 Switch:
```javascript
import { Switch } from 'react-router';
```
如果你仍然想从 react-router-dom 中导入 Switch,你需要将 React Router 升级为 v6 版本。如果你使用的是旧版本的 React Router,那么你需要从 react-router-dom 中导入 Switch:
```javascript
import { Switch } from 'react-router-dom';
```
希望这可以帮助你解决问题。
export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'
The error message you encountered suggests that the named export 'withRouter' was not found in the 'react-router-dom' package. This error commonly occurs when using the latest versions of React Router, as the 'withRouter' higher-order component has been deprecated.
Instead of using 'withRouter', you can access the router props by using the 'useHistory', 'useLocation', or 'useParams' hooks provided by React Router. These hooks allow you to access the history, location, and params objects directly within your functional components.
For example, to access the history object, you can use the 'useHistory' hook:
```jsx
import { useHistory } from 'react-router-dom';
function MyComponent() {
const history = useHistory();
// Access history methods (e.g., push, replace, goBack) here
// ...
}
```
Similarly, you can use the 'useLocation' and 'useParams' hooks to access the location and params objects, respectively.
Please note that these hooks are only available in functional components. If you are using a class component, you might need to consider using a different approach or wrapping your class component with a functional component that utilizes these hooks.
阅读全文