import React from 'react' import {connect} from 'react-redux' import { Redirect} from 'react-router-dom' import axios from 'axios' import {login} from './Auth.redux.js' //兩個reducers 每一個reducers都有一個state @connect( (state)=>state.auth, {login} ) class Auth extends React.Component{ constructor(props){ super(props) this.state={ data:{}, success:false //解決方法設置一個開關,當數據請求成功,置爲true } } componentDidMount(){ axios.get('/data') .then(res=>{ if(res.status===200){ this.setState({data:res.data,success:true}) console.log(this.state.data[0].user) debugger } }) } render(){ return ( <div> <h2>個人名字是{this.state.success?this.state.data[0].user:''}</h2> {this.props.isAuth?<Redirect to='/dashboard'></Redirect>:null} <h2>你沒有權限須要登陸</h2> <button onClick={this.props.login}>登陸</button> </div> ) } } export default Auth