我正在尝试使用redux来构建我的第一个小型Web应用程序并做出反应,我正在按照一门课程做他的工作,因此我可以学习如何设置,但是我的控制台出现错误:
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
这是我的简化版/用户列表文件:
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();
任何帮助将不胜感激。
答案 0 :(得分:0)
您需要包装Provider
并将商店添加为道具。请参阅docs。在app.js中:
<Provider store={store}>
<div>
<UserList />
</div>
</Provider>
答案 1 :(得分:0)
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中,并将存储作为props添加到index.js中