javascript - 如何通过异步函数设置 react 父组件状态,然后将此状态作为 Prop 传递给子组件?

标签 javascript reactjs async-await react-props react-lifecycle

所以我想通过 firebase 函数设置访问权限,然后将此访问 Prop 作为 Prop 传递给选项卡组件,但选项卡组件的初始状态为 null,之后 firebase 身份验证功能正在解析。

class Admin extends React.Component {
  state = {
    access: null,
  };
  componentDidMount() {
    this.unListen = firebase.auth().onAuthStateChanged(user => {
      if (user) {
        this.setState(() => ({ access: true }));

      }
    });
  }

  componentWillUnmount() {
    this.unListen();
  }

render(){
return <Tab access={this.state.access}/>
  }
}

最佳答案

你可以做到 conditional rendering在您获得访问权限之前不呈现选项卡:

return this.state.access 
    ? <Tab access={this.state.access}/> 
    : <div>Not authorized</div>

关于javascript - 如何通过异步函数设置 react 父组件状态,然后将此状态作为 Prop 传递给子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465240/

相关文章:

javascript - 更改数组的 div 结构

javascript - Node.JS Airtable - Await 不等待 promise 被解决

javascript - 如何访问 jQuery 事件中的对象方法

javascript - 如何在 Jasmine/Maven 自动生成的 Manual SpecRunner.html 中配置我的 javascript 文件的路径?

javascript - AngularJS - 使用指令调用回调函数?

c# - 等待来自多个对象的任务

javascript - 在 Angular 中,使用 async/await 时如何处理 promise 拒绝

javascript - 如何防止在 Html 输入(组合框)中显示 val

javascript - react : Suggestion is not showing in the drop-down

javascript - React 编译失败。找不到模块 : Can't resolve, 如何使用react-router进行编译?