Javascript React redux状态未定义

Javascript React redux状态未定义,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我对redux是个新手,对两者都有反应。我正在试用我的第一个redux应用程序,其中我的状态被传递为未定义。虽然我在互联网上搜索了一下,想找出问题所在,但没有找到问题所在 下面是代码片段- Init.js export default function() { return { isLoggedIn: false, username: "admin" }; } Reducer.js import initialState from "./init"; let cu

我对redux是个新手,对两者都有反应。我正在试用我的第一个redux应用程序,其中我的状态被传递为未定义。虽然我在互联网上搜索了一下,想找出问题所在,但没有找到问题所在

下面是代码片段-

Init.js

export default function() {
   return {
     isLoggedIn: false,
     username: "admin"
  };
}
Reducer.js

import initialState from "./init";
let currentState = initialState;
export default function(state = currentState, action) {
  if (action.type === "ONLICK") {
    return state;
  } else {
    return state;
  }
}
Index.js

const allReducers = combineReducers({
  currentstate: currentState
});

export default allReducers;
currentState.js

import React, { Component } from "react";
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { allreducers } from "./../reducer/index";

class CurrentState extends Component {
  render() {
    console.log("Logged In-" + this.props.isLoggedIn);
    return <div>{this.props.isLoggedIn}</div>;
  }
}

function mapStateToProps(state) {
  return {
    isLoggedIn: state.isLoggedIn
  };
}

export default connect(mapStateToProps)(CurrentState);
import React,{Component}来自“React”;
从“redux”导入{bindActionCreators};
从“react redux”导入{connect};
从“/./reducer/index”导入{allreducers};
类CurrentState扩展组件{
render(){
log(“登录-”+this.props.isLoggedIn);
返回{this.props.isLoggedIn};
}
}
函数MapStateTops(状态){
返回{
isLoggedIn:state.isLoggedIn
};
}
导出默认连接(MapStateTops)(当前状态);
navbar.js

import React, { Component } from "react";
import CurrentState from "./../container/currentstate";
import "bootstrap/dist/css/bootstrap.css";

class Navbar extends Component {
  constructor() {
    super();
  }
  render() {
    return (
      <div>
        <h3>State -</h3>
        <CurrentState />
      </div>
    );
  }
}
import React,{Component}来自“React”;
从“/./容器/CurrentState”导入CurrentState;
导入“bootstrap/dist/css/bootstrap.css”;
类导航栏扩展组件{
构造函数(){
超级();
}
render(){
返回(
陈述-
);
}
}

在Navbar.js中显示状态的一个属性。但在控制台中,当我试图打印值时,它显示为未定义。

您在mapStateToProps中以错误的方式获取状态。

使用

反而


希望对您有所帮助

init.js
返回的代码是一个函数。分配
currentState
时是否应该调用该函数<代码>让currentState=initialState();您还导入了“/init”,但作为标题,您写的是“init.js”是大写还是小写?init.js是小写的,只是问题在于它是一个打字错误。此外,即使我使用let currentState=initialState(),输出也保持不变。是否在index.js中导入
currentState
?是的,否则应该说currentState未定义。
function mapStateToProps(state) {
  return {
    isLoggedIn: state.currentstate
  };
}
function mapStateToProps(state) {
      return {
        isLoggedIn: state.isLoggedIn
      };
}