【问题标题】:Async React component lifecycle methods异步 React 组件生命周期方法
【发布时间】:2020-05-10 00:44:57
【问题描述】:

如果 React 组件上的生命周期方法标记为 async,它们的处理方式是否不同?

    default class MyComponent extends Component {
      state = {};

      async componentDidMount() {
        await this.fillQuestions();
      }
    }

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

它们被调用的方式没有区别。 async 仅表示该函数将返回 Promise。 由于 React 不期望任何返回值,因此从 React 的角度来看,两者之间没有区别。

但是,当您想要 await 用于生命周期挂钩中的其他承诺时,您可以使用 async

请看以下中篇文章: https://medium.com/front-end-weekly/async-await-with-react-lifecycle-methods-802e7760d802

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多