componentDidMount如何在执行之前(不使用setTimeout)等待redux存储中的值?

我试图将身份验证信息放入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返回值

a16592664 回答:componentDidMount如何在执行之前(不使用setTimeout)等待redux存储中的值?

前一段时间我遇到了同样的问题,而帮助我的是将组件视为功能。

我要做的是显示一个微调框,直到auth不为null,然后在准备好后渲染另一个视图。

    class Dashboard extends Component {    
       render = () => {
             if(this.props.auth === null){
                 return <SpinnerComponent />
             }

             return ....
       }
    }

会发生什么:

  • 在props.auth准备就绪之前,组件将渲染微调器
  • props.auth准备就绪后,该组件将呈现您的常规应用
  • 在更改道具(即更改redux)时进行渲染
本文链接:https://www.f2er.com/3155265.html

大家都在问