javascript - 无法使用 mapStateToProps 访问存储

标签 javascript reactjs react-redux

我是新手,我正在尝试使用 redux 设置登录系统。在我的登录组件中,我将 mapStateToPropsreact-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/

相关文章:

javascript - 在 ui-grid angularjs 中显示 Json 响应

reactjs - 将 setstate 钩子(Hook)函数传递给子路由组件。解构错误

javascript - 将 Google Hire 脚本添加到 Gatsby 站点

javascript - 在 React Native 中使用样式化组件捕获 Pressable press

javascript - React-Router npm 包在控制台中给我警告? (不推荐使用 PropTypes)

react-native - 在 react 导航中使用 navigation.goBack 发送参数

javascript - 我应该将 ReactJS 组件的 onClick 逻辑放在什么位置?

javascript - 如何在 jquery 中编写正则表达式来查找具有有效域名的有效 url 格式

javascript - Razor 在使用 Javascript 时失败

javascript - Typescript 推断高阶函数的类型,该函数执行具有相同参数签名的可选函数参数