gpt4 book ai didi

reactjs - withRouter' 不是从'react-router-dom 导出的

转载 作者:行者123 更新时间:2023-12-04 14:28:39 30 4
gpt4 key购买 nike

在 npm i --save react-router-dom 和 npm install --save with-router 之后我试着写

import {withRouter} from 'react-router';
但我收到这个错误
尝试导入错误:“withRouter”未从“react-router”导出。


import React from 'react';
import PropTypes from 'prop-types';
import { Formik } from 'formik';
import {
Box,
Button,
Card,
CardContent,
CardHeader,
Divider,
} from '@material-ui/core';
import { connect } from 'react-redux';
import jsonGR from "src/assets/data/greek.json";
import jsonEN from "src/assets/data/english.json";
import { LAN_EN } from 'src/actions/types';
import CloudUploadIcon from '@material-ui/icons/CloudUpload';
import AddIcon from '@material-ui/icons/Add';
import axios from 'axios';
import LinearProgress from '@material-ui/core/LinearProgress';
import Typography from '@material-ui/core/Typography';
import { withRouter } from 'react-router'

class ProfileDetails extends React.Component {
//code
}
};
ProfileDetails.propTypes = {
className: PropTypes.string
};

const mapStateToProps = state => {
return { loginsession: state.loginsession,
selectedlan: state.selectedlan };
};

export default withRouter(ProfileDetails);
文件 package.json 包含我在项目中安装 npm 的依赖项以及所有必要的信息。我不明白我尝试了很多方法但没有人工作的问题在哪里
{
"name": "react-material-dashboard",
"author": "Apanay22",
"licence": "MIT",
"version": "1.0.0",
"private": false,
"scripts": {
"start": "react-scripts start http-server ",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "^4.0.0-alpha.56",
"@material-ui/styles": "^4.10.0",
"axios": "^0.21.1",
"bcrypt": "^5.0.0",
"chart.js": "^2.9.3",
"clsx": "^1.1.1",
"compress.js": "^1.1.2",
"cors": "^2.8.5",
"csv-parse": "^4.15.1",
"express": "^4.17.1",
"formik": "^2.1.5",
"glob": "^7.1.6",
"gulp": "^4.0.2",
"history": "^5.0.0",
"lodash": "^4.17.19",
"material-ui-popup-state": "^1.7.1",
"moment": "^2.27.0",
"mui-datatables": "^3.7.6",
"nprogress": "^0.2.0",
"papaparse": "^5.3.0",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-chartjs-2": "^2.10.0",
"react-csv-reader": "^3.2.1",
"react-dom": "^16.13.1",
"react-feather": "^2.0.8",
"react-helmet": "^6.1.0",
"react-hot-toast": "^1.0.2",
"react-image-file-resizer": "^0.4.2",
"react-navigation": "^4.4.4",
"react-perfect-scrollbar": "^1.5.8",
"react-redux": "^7.2.2",
"react-router": "^6.0.0-beta.0",
"react-router-dom": "^6.0.0-beta.0",
"react-scripts": "^3.4.1",
"react-toast-notifications": "^2.4.0",
"react-toastify": "^7.0.2",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"use-history": "^1.4.1",
"uuid": "^8.3.0",
"with-router": "^1.0.1",
"yup": "^0.29.3"
},
"devDependencies": {
"@types/react-router-dom": "^5.1.7",
"concurrently": "^5.3.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^2.5.1",
"prettier": "^1.19.1"
},
"proxy": "http://localhost:9000"
}

最佳答案

你需要做

import { withRouter } from 'react-router'
https://reactrouter.com/web/api/withRouter
如果你有上面的代码,你不应该得到这个错误:
尝试导入错误:“withRouter”未从“react-router-dom”导出。
因为您不再尝试从 'react-router-dom' 导入。如果你仍然看到那个错误,我会做一个项目搜索 'react-router-dom' 并确保你没有试图将它导入到其他地方。
关于 package.json 的评论
如果您查看 package.json,就会发现 react-router 和 react-router-dom 的版本与 types/react-router-dom 的版本不同。不确定这是否相关,但无论如何都可以研究一下。

关于reactjs - withRouter' 不是从'react-router-dom 导出的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66465750/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com