我正在尝试使用 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();
任何帮助将不胜感激。
最佳答案
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/