javascript - 在 "store"的上下文中找不到 "Connect(MyComponent)"吗? Redux-React 问题

标签 javascript reactjs redux react-redux

我正在尝试使用 redux 和 React 构建我的第一个小型 Web 应用程序,我正在学习一门类(class)并完全按照他所做的那样,这样我就可以学习如何设置,但我的控制台中出现错误:

Could not find "store" in the context of "Connect(UserList)"

现在我将解释我所做的事情,我制作了这个简单的文件夹结构:

reactapp --- src --- actions ( empty for now )

                 --- components --- app.js
                                --- userlist.js

                 --- reducers   --- index.js
                                --- userlist.js

                 index.js

这是我的简单 reducer /用户列表文件:

export default function() {
  return [
    { id: 1, nom: "Taoufiq BENALLAH", active: "NON", role: "Internship" },
    { id: 2, nom: "Mounia Drari", active: "OUI", role: "Developer" },
    { id: 3, nom: "Jaouhar Alaoui", active: "NON", role: "Internship" },
    { id: 4, nom: "ILyass LAZ", active: "OUI", role: "Technical Expert" }
  ];
}

这是我的reducers/index.js 文件:(我的输入设置正确)

import { combineReducer } from "redux";
import UserList from "./userlist";

const rootReducer = function() {
   users: UserList;
};

export default rootReducer;    

这是我的组件用户列表:

import React, { Component } from "react";
import { connect } from "react-redux";

class UserList extends Component {
   render() {
     return <div>{this.props.users}</div>;
   }
}

function mapStateToProps(state) {
   return {
     users: state.users
   };
}

export default connect(mapStateToProps)(UserList);

这是我的 app.js

import React, { Component } from "react";
import UserList from "./userlist";

class App extends Component {
  render() {
    return (
      <div>
        <UserList />
      </div>
    );
  }
}

export default App;

最后,像往常一样,这是我的 src/index.js :

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./components/app";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(<App />, document.getElementById("root"));

serviceWorker.unregister();

这是我的错误的详细信息: enter image description here

任何帮助将不胜感激。

最佳答案

import { createStore, applyMiddleware, combineReducers } from 'redux';
import logger from 'redux-logger'

const store = createStore(rootReducer, applyMiddleware(logger));

ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));

您需要将 App 包装在 Provider 中,并在 index.js 中添加 store 作为 props

关于javascript - 在 "store"的上下文中找不到 "Connect(MyComponent)"吗? Redux-React 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54628062/

相关文章:

javascript - 无法使用谷歌图表适应模式窗口

javascript - 过滤 redux 状态时收到错误消息

javascript - React 组件不工作

javascript - 中止在 Redux 中获取数据

c# - 页面加载后脚本不工作

Javascript对象内部自引用

javascript - 与 d3.svg.path() 的键绑定(bind)

javascript - React 表单验证 - 回调而非事件

reactjs - 路由更改时更新 Redux 状态

reactjs - redux-saga 中的重试功能