redux 有人能帮我解决这个React问题吗

yvgpqqbh  于 2022-12-19  发布在  React
关注(0)|答案(1)|浏览(165)

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编写

nfzehxib

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 ConceptsFAQ,以充分了解更改内容以及如何更好地使用RRDv6。

相关问题