我是新手,我正在尝试使用 redux 设置登录系统。在我的登录组件中,我将 mapStateToProps
与 react-redux
提供的 connect
方法结合使用。
当我试图从商店获取我需要的东西时,它一直说它是未定义的。这是我的登录组件的片段:
function mapStateToProps(state) {
return {
loggingIn: state.authentication.loggedIn,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(userActions, dispatch),
alertActions: bindActionCreators(alertActions, dispatch),
};
}
export default connect(mapStateToProps, mapDispatchToProps)(Login)
以下是我尝试组合 reducer 的方法:
import { combineReducers } from 'redux';
import authentication from './userReducer';
import alert from './alertReducer'
const rootReducer = () => combineReducers({
authentication,
alert
});
export default rootReducer;
但是我无法访问登录组件中的 logginIn
属性。经过几个令人沮丧的时间的故障排除后,我通过删除箭头功能使其正常工作:
const rootReducer = combineReducers({
有人可以告诉我为什么箭头功能不起作用吗?谢谢
更新:这是我在 index.js 文件中导入根 reducer 的方法
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { Provider } from 'react-redux'
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk';
import rootReducer from './reducers'
const store = createStore(rootReducer, applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root'))
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();
最佳答案
docs告诉你调用combineReducers来提供根reducer。 createStore 方法需要一个化简器,而不是调用来获取此化简器的函数。
关于javascript - 无法使用 mapStateToProps 访问存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839869/