简体   繁体   English

如果组件代码有错误,仍然呈现页面 - React/Nextjs

[英]Still render the page if the components code has error - React/Nextjs

I haven't got much experience in React, would really appreciate any advice.我在 React 方面没有太多经验,非常感谢任何建议。 My situation is that:我的情况是:

  • I have a dynamic page let say the url is mysite/[productid]我有一个动态页面,假设 url 是 mysite/[productid]
  • The page is the combination of component A, B and C页面是组件A、B和C的组合
  • There is an error happened in component B and React render 404 page which I don't want to.组件 B 和 React 渲染 404 页面中发生错误,我不想这样做。

My question is: Is there any way in React/Nextjs to still render the page (the page still displays without the error component) if one the components scratch, and log the error using Sentry?我的问题是:React/Nextjs 中是否有任何方法可以在组件刮擦时仍然呈现页面(页面仍然显示而没有错误组件),并使用 Sentry 记录错误? As I know there is a way to do that with Emberjs.据我所知,有一种方法可以使用 Emberjs 来做到这一点。

I do not want to render another error page with error boundary as it does not make sense to take down whole page with only one error component.我不想呈现另一个带有错误边界的错误页面,因为只用一个错误组件来删除整个页面是没有意义的。

Thank you in advance.先感谢您。

There are two solutions to handling errors in a React application:在 React 应用程序中处理错误有两种解决方案:

If a component can recover from an error internally, then the expressions which may throw that error should be wrapped in a try/catch block.如果组件可以从内部错误中恢复,那么可能抛出该错误的表达式应该包含在try/catch块中。 Otherwise, you can use React Error Boundaries to manage any errors in child components.否则,您可以使用 React 错误边界来管理子组件中的任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM