我想要构建一个React组件,在 componentDidMount
生命周期异步加载数据。
以下是当前函数的代码(使用 TypeScript
编写):
async componentDidMount(): Promise<void> {
try {
const { props: { teamId }, state: { } } = this;
const awaitableData = await UrlHelper.getDataAsync("some-fancy-url");
// ... do something with awaitableData
} catch(e) {
console.log("Some error occured");
throw e;
}
}
render
函数返回用ErrorBoundary
组件包装的标记,该组件已经实现了componentDidCatch
。但是,在等待的调用被拒绝时,这个函数从未被调用或触发,最终进入了catch
块中。我错过了什么?
try/catch
但仍然无法触发componentDidCatch
。只有在开发工具中,我才能看到来自拒绝承诺的部分reject(new Error(xhr.statusText))
的Uncaught (in promise) Error: Internal Server Error
。 - KingKerosincomponentDidCatch
从未被触发。 - KingKerosin