Javascript 组件中的空状态(react redux)

Javascript 组件中的空状态(react redux),javascript,reactjs,redux,Javascript,Reactjs,Redux,我的组件中的状态有问题。 我正试图从我的reducer中获取状态,但状态为空,仅获取未定义 这是我的actionCreator export function checkLogin() { return function(dispatch){ return sessionApi.authCheck().then(response => { dispatch(authSuccess(true)); }).catch(error => {

我的组件中的
状态有问题。
我正试图从我的reducer中获取状态,但
状态
为空,仅获取
未定义

这是我的
actionCreator

export function checkLogin() {
return function(dispatch){
    return sessionApi.authCheck().then(response => {
        dispatch(authSuccess(true)); 
    }).catch(error => {
        throw(error)
    })
 }
}
我的减速机

export const authStatus = (state = {}, action) => {
switch(action.type){
    case AUTH_FALSE:
            return{
                status: action.status
            }
    case AUTH_TRUE:
            return {
                ...state,
                status: action.status
            };
    default:
        return state;
  }
};
这是我的组件,我正在尝试获取状态

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

const mapDispatchToProps = (dispatch:any) => {
  const changeLanguage = (lang:string) => dispatch(setLocale(lang));
  const checkAuth = () => dispatch(checkLogin());
  return { changeLanguage, checkAuth }
};

@connect(mapStateToProps, mapDispatchToProps)
我需要从州政府获得
状态

组件

import * as React from "react";
import  Navigation  from './components/navigation';
import { connect } from 'react-redux';
import { setLocale } from 'react-redux-i18n';
import cookie from 'react-cookie';
import {checkLogin} from "./redux/actions/sessionActions";

class App extends React.Component<any, any> {
  constructor(props:any) {
    super(props);
    this.state = {
      path: this.props.location.pathname
    };
  }
  componentDidMount(){
    this.props.checkAuth();
    this.props.changeLanguage(cookie.load('lang'));
  }
  componentWillUpdate(){
  }

  render() {
    return (
      <div>
        <Navigation path={this.state.path} />
          {this.props.children}
      </div>
    )
  }

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

const mapDispatchToProps = (dispatch:any) => {
  const changeLanguage = (lang:string) => dispatch(setLocale(lang));
  const checkAuth = () => dispatch(checkLogin());
  return { changeLanguage, checkAuth }
};

@connect(mapStateToProps, mapDispatchToProps)
export class Myapp
extends App {}
import*as React from“React”;
从“./components/Navigation”导入导航;
从'react redux'导入{connect};
从'react-redux-i18n'导入{setLocale};
从“反应cookie”导入cookie;
从“/redux/actions/sessionActions”导入{checkLogin};
类应用程序扩展了React.Component{
构造器(道具:任何){
超级(道具);
此.state={
路径:this.props.location.pathname
};
}
componentDidMount(){
this.props.checkAuth();
this.props.changeLanguage(cookie.load('lang');
}
componentWillUpdate(){
}
render(){
返回(
{this.props.children}
)
}
}
常量mapStateToProps=(状态)=>{
返回{
状态:state.status
}
};
const mapDispatchToProps=(调度:任意)=>{
constchangelanguage=(lang:string)=>dispatch(setLocale(lang));
const checkAuth=()=>dispatch(checkLogin());
返回{changeLanguage,checkAuth}
};
@连接(MapStateTrops、mapDispatchToProps)
导出类Myapp
扩展应用程序{}

您无法访问
构造函数内部异步的道具。因为当您实例化组件时,
构造函数将只执行一次。当您实例化组件时,您的异步调用尚未响应,因此
this.props.status
未定义

您可以使用React生命周期方法中的组件willreceiveprops
,例如:

componentWillReceiveProps(nextProps) {
  console.log(nextProps.status);
}
每次连接或传递到组件的道具发生更改时,都将执行此方法。 您还可以在
渲染
中使用
this.props.status
,因为每次更改道具时也会执行此操作


为了更好地理解react lifecycle,您可以在此处查看不同的可用方法:

一旦您使用
mapStateToProps
将状态传递给组件道具,就应该可以通过组件道具访问状态。您正在尝试访问
此.props.status
?你应该展示完整的组件,这样人们就可以理解你的问题。是的,我正试图从
这个.props.status
未定义的
中获取数据。我不知道为什么会这样。可能有很多原因,很难说?您是否将减速机包含在组合减速机功能中?您是否匹配了减速器中的任何案例?此外,您还忘记了
…状态,
,以防AUTH\u FALSE。这可能是使用
componentWillReceiveProps(next){next.state}