reactjs - MapStateToProps 给我未定义的错误

标签 reactjs typescript react-redux

我正在尝试像这样使用mapStateToProps:

const mapStateToProps = (state: { logedIn: boolean; } ) => {
return {
    logedIn: state.logedIn
}
}

我正在创建 typescript react 应用程序。 我尝试在编辑器中使用 ctrl 搜索来搜索logedIn,并且发现它处于应该处于的初始状态。 但是当我执行 npm start 时,我收到此错误:

  TypeError: Cannot read property 'logedIn' of undefined
Function.mapStateToProps [as mapToProps]
src/components/LogIn/Login.tsx:79
  76 | 
  77 | const mapStateToProps = (state: { logedIn: boolean; } ) => {
  78 |     return {
> 79 |         LogedIn: state.logedIn
  80 |     }
  81 | }
  82 | 

这是我的初始状态

interface IAppState {
    logedIn : Boolean;
}

const initialState : IAppState = {
    logedIn  : false
};

 const reducer = (state = initialState, action : any) => {
    switch (action.type) {
        case actionTypes.LOG_IN:
            return {
                ...state,
                logedIn: true
            }
        case actionTypes.LOG_OUT:
            return {
             ...state,
             logedIn: false
            }
    }
}

创建商店:

export const store = createStore(reducer)

我在这里也收到错误,它说没有重载与“reducer”匹配

reducer :

   import {actionTypes} from "./actionTypes";

 export const LogedIn = () => {
    return {
        type:actionTypes.LOG_IN
    }
}

export const LogOut = () => {
    return {
        type:actionTypes.LOG_OUT
    }
}

请问有什么建议吗?我真的被这些困住了

最佳答案

createStore接受初始状态作为第二个参数:

createStore(reducer, [preloadedState], [enhancer])

在您的情况下,您没有在商店上签名:

export const store = createStore(reducer, initialState)

关于reactjs - MapStateToProps 给我未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62672257/

相关文章:

reactjs - Redux with React - 与组件共享存储的正确方法

reactjs - react 网格布局与 typescript 不工作

jquery - 在 Brunch 2.8 中加载 JQuery 插件

javascript - 在redux中发送action,如果switch中的reducer指令有多种情况

Angular Material2 日期选择器 - 动态格式化

typescript - 如何为 lodash/fp 管道传输 Promise.allSettled? (或任何类似管道库的功能,例如 : p-pipe)

javascript - 警告 : Failed propType: Required prop `dimensionName` was not specified in `DimensionPicker` . 检查 `Connect(DimensionPicker)` 的渲染方法

reactjs - process.env 在哪里

reactjs - createBottomTabNavigator 具有针对不同选项卡的动态 tabStyle

css - 使用 CSS + React + Semantic UI 更改占位符颜色