Auth.js
import React from "react"
import { connect } from 'react-redux'
import { Redirect } from 'react-router-dom'
import { login } from './Auth.redux'
import axios from 'axios'
class Auth extends React.Component{
constructor(props) {
super(props);
this.state = {
data:{}
};
}
componentDidMount(){
axios.get('/data')
.then(res=>{
if(res.status == 200){
this.setState({data:res.data})
console.log(res)
console.log(this.state.data)
}
})
}
render(){
if(!this.state.data.length){
return null
}
console.log(this.props.auth.isAuth)
return(
<div>
<h2>ss{this.state.data[1].user}</h2>
{ this.props.auth.isAuth ? <Redirect to='/dashboard' /> :null}
<h1>需等登录</h1>
<button onClick={this.props.login}>登录</button>
</div>
)
}
}
const mapStateToProps=(state)=>{
return (state:state.auth)
}
const actionCreators = {login}
Auth = connect(mapStateToProps,actionCreators)(Auth)
export default Auth