【发布时间】:2018-06-06 19:20:15
【问题描述】:
在 React Native 中使用 componentDidMount() 作为异步函数的良好做法还是应该避免它?
当组件挂载时,我需要从AsyncStorage 获取一些信息,但我知道使这成为可能的唯一方法是使componentDidMount() 函数异步。
async componentDidMount() {
let auth = await this.getAuth();
if (auth)
this.checkAuth(auth);
}
这有什么问题吗?还有其他解决方案吗?
【问题讨论】:
-
“良好做法”是一个见仁见智的问题。它有效吗?是的。
-
这是一篇很好的文章,它说明了为什么 async await 是一个比 promise 更好的选择 hackernoon.com/…
-
使用 redux-thunk 就可以解决问题
-
@TilakMaddy 为什么你认为每个 React 应用都使用 redux?
-
@Mirakurun 为什么当我过去问普通的javascript问题时,整个堆栈溢出都假设我使用jQuery?
标签: reactjs asynchronous react-native