在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ isAuth修改后頁面沒跳轉?

isAuth修改后頁面沒跳轉?

在login頁面點擊登錄后修改了isAuth為true,但頁面沒有跳轉到dashboard?

Auth.js

import React, { Component } from 'react'
import { Redirect } from 'react-router-dom'
import { connect } from 'react-redux'

import { login } from './Auth.redux'

// @connect(
//     state => state
// )
class Auth extends Component{
    handleClickLogin(){
        this.props.login()
    }
    render(){
        //console.log(this.props)
        return(
            <div>
                {this.props.isAuth ? <Redirect to='/dashboard' /> : null}
                <p>你沒有權限查看,需要登錄</p>
                <button onClick={() => this.handleClickLogin()}>登錄</button>
            </div>
        )
    }
}

const mapStateToProps = state => {
    return {
        auth: state.auth
    }
}

const authCreators = {login}

export default connect(mapStateToProps, authCreators)(Auth)

Auth.redux.js

const LOGIN = 'login'
const LOGOUT = 'logout'

const initialState = {
    isAuth: false,
    username: 'ok'
}

export function auth(state=initialState, action){
    switch(action.type){
        case LOGIN:
            return {...state, isAuth: true}
        case LOGOUT:
            return {...state, isAuth: false}
        default:
            return state
    } 
}

export function login(){
    return {type: LOGIN}
}

export function logout(){
    return {type: LOGOUT}
}

圖片描述

圖片描述

回答
編輯回答
撥弦

Auth.js和Dashboard.js里面從store里拿isAuth出錯了.因為isAuth是放在auth這個對象里面所以取得時候應該是isAuth: state.auth.isAuth

2017年8月23日 00:46