我试图将身份验证信息放入React组件并获取与已身份验证的用户有关的数据,但是除非使用setTimeOut,否则componentDidMount不会从redux存储中获取auth值。 我该怎么办?
我尝试了componentWillReceiveProps(),但这也不起作用。
class Dashboard extends Component {
componentDidMount = () => {
console.log(this.props.auth);
setTimeout(
() => console.log(this.props.auth),
100
)
}
setTimeout中只有console.log返回值
答案 0 :(得分:0)
前一段时间我遇到了同样的问题,而帮助我的是将组件视为功能。
我要做的是显示一个微调框,直到auth不为null,然后在准备好后渲染另一个视图。
class Dashboard extends Component {
render = () => {
if(this.props.auth === null){
return <SpinnerComponent />
}
return ....
}
}
会发生什么: