我是靠谱客的博主 风中大白,这篇文章主要介绍react之异步请求数据,render先行渲染报错,未拿到数据,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

转载于:https://www.cnblogs.com/raind/p/9607491.html

最后

以上就是风中大白最近收集整理的关于react之异步请求数据,render先行渲染报错,未拿到数据的全部内容,更多相关react之异步请求数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部