问题描述
我的用例是:在我的 react 应用程序的顶层,我想捕获任何错误,清除缓存(以防止任何与数据相关的错误持续存在),然后无论如何抛出错误,以便应用程序崩溃。
这可能并不像听起来那么奇怪,因为我正在使用 React Native。在移动应用中,很常见的错误只是让应用崩溃,这样您就可以再次打开它。
我已经构建了这个:
class TopLevelErrorBoundary extends React.Component<Props,State> {
constructor(props: Props) {
super(props)
this.state = { hasError: false,hasClearedCache: false }
}
static getDerivedStateFromError(error: Error) {
return { hasError: true,error }
}
async clearCacheAndraiseerror(error: Error) {
await cache.clear()
throw error
}
componentDidCatch(error: Error): void {
this.clearCacheAndraiseerror(error)
}
render() {
const { hasError,error } = this.state
if (hasError && error) {
return null
}
return this.props.children
}
}
然而,再次抛出错误似乎没有任何作用 - 看起来它只是挂在 null
状态。
有没有办法做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)