export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'
时间: 2023-10-20 14:33:11 浏览: 72
react-native-uuid:UUID生成,用于本机
5星 · 资源好评率100%
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.
阅读全文