React Native componentDidMount 生命周期性能问题

React Native componentDidMount lifecycle performance issue

我正在异步使用 componentDidMount,例如:

async componentDidMount() {
  const res0 = await fetch('something')
  const res1 = await fetch('somethingElse')
}

想知道这是否会导致任何性能问题,或者这是不好的做法,有更好的方法吗?

提前致谢

是的。你可以做到这一点。

componentDidMount 本身就是为发出 API 请求而构建的,因此使用 async / await 没有坏处。

我建议你不要这样做。我们永远不应该改变/改变已经定义的东西。

相反,您可以创建一个单独的 async 函数并在 componentDidMount 中调用该函数。

componentDidMount() {
  this.fetchData();
}

函数应该是,

fetchData = async () => {
  const res0 = await fetch('something')
  const res1 = await fetch('somethingElse')
}