error am getting now
这是index.js文件
import React from 'react';
import ReactDOM from 'react-dom/client';
import 'bootstrap/dist/css/bootstrap.min.css'
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter as Router} from 'react-router-dom'
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.Fragment>
<Router>
<App />
</Router>
</React.Fragment>
);
reportWebVitals();
这是我导入ConfigureStore.js文件的App.js
import './App.css';
import Main from './components//MainComponent';
import React,{ Component } from 'react';
import { Provider } from 'react-redux'
import { ConfigureStore } from './redux/configurStore'
const store= ConfigureStore()
class App extends Component {
render(){
return (
<Provider store={store}>
<div className="App">
<Main />
</div>
</Provider>
);
}
}
export default App;
这是用于configerstor.js文件的reducer.js文件
import { DISHES } from "../shared/dishes"
import { COMMENTS } from '../shared/comments';
import { PROMOTIONS } from '../shared/promotiones';
import { LEADERS } from '../shared/leaders'
export const initialState = {
dishes: DISHES,
comments: COMMENTS,
promotions: PROMOTIONS,
leaders: LEADERS
};
export const Reducer = (state = initialState, action) => {
return state ;
};
这是configurstor.js文件
import createStore from 'redux';
import { Reducer, initialState } from './reducer'
export const ConfigureStore = () => {
const store = createStore(
Reducer,
{},
initialState
);
return store;
}
这是主组件. js文件
import { Routes, Route } from "react-router-dom";
import { connect } from 'react-redux';
function withRouter(Component) {
function ComponentWithRouterProp(props) {
let location = useLocation();
let navigate = useNavigate();
let params = useParams();
return (
<Component
{...props}
location={location}
params={params}
navigate={navigate}
/>
);
}}
const mapStateToProps = state => {
return {
dishes: state.dishes,
comments: state.comments,
promotions: state.promotions,
leaders: state.leaders
}
}
class Main extends Component {
constructor(props) {
super(props);
}
render(){
}
}
export default withRouter(connect(mapStateToProps)(Main));
我尝试从react-router-dom导入withouter,但它显示此错误
error when i import withRouter
所以我用Router编写
1条答案
按热度按时间nfzehxib1#
看起来您在导入和依赖项方面有一些问题。
1.您***默认***导入Redux并将其命名为
createStore
,而不是导入***命名的***createStore
导出。代替
import createStore from 'redux';
它应该是
import { createStore } from 'redux';
1.您似乎安装了
react-router-dom
的最新版本,即版本6,该版本不再导出任何withRouter
高阶分量。如果您打算继续使用
withRouter
HOC,则需要恢复到RRDv5。在终端的根项目目录中运行以下命令以安装旧版本并更新package.json文件:npm install --save react-router-dom@5
如果您打算继续使用RRDv6,则需要解决版本之间的任何重大更改。删除
import { withRouter } from 'react-router-dom';
并遵循Upgrading from v5迁移指南。您还需要通读Main Concepts和FAQ,以充分了解更改内容以及如何更好地使用RRDv6。