访问Redux时无法定义状态存储在React App中

时间:2018-12-26 07:47:42

标签: reactjs redux react-redux

我在我的react应用程序中使用redux,当我在一个组件中访问redux state属性时却变得未定义,为什么呢?当我在reducer文件中调用console.log时,该状态有效:这是我的reducerFile:

const initState = {
    isCurrentUser : true
}
export default function(state=initState, action) {


    console.log(`this is from localAuthReducer ${state.isCurrentUser}`)
    switch(action.type) {
        default: 
            return state
    }
}

这是我的反应组件:

import React, {Component} from 'react';
import styles from './IndexPage.module.scss';
import { connect } from 'react-redux';
import Header from './../../components/Header/Header';



class IndexPage extends Component {


render() {
       return(

           <div className={styles.container}>


                <Header

                 isCurrentUser = {this.props.isCurrentUser}
                 />

                { console.log(`this is from indexPage ${this.props.isCurrentUser}`)}


           </div>

       );
   }
}

function mapStateToProps(state) {
    return {
        isCurrentUser : state.isCurrentUser
    }
}


export default connect(mapStateToProps, null)(IndexPage);

这是我的index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App/App';
import {Provider } from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import reducers from './reducers/index';
import reduxThunk from 'redux-thunk';

const store = createStore(
    reducers,
    applyMiddleware(reduxThunk)
);




ReactDOM.render(
                <Provider store={store}>
                    <App />
                </Provider>
                , document.querySelector("#root"));

我不知道哪里出问题了isCurrentUser必须具有true值,因为它是redux状态的默认值

0 个答案:

没有答案