[英]React JS, stop errors displaying in production
So recently we launched our first React JS application online. 因此,最近我们在线启动了我们的第一个React JS应用程序。 However, when people open the site, then move to a different tab and close their computer, an error message displays when they come back.
但是,当人们打开站点,然后移至其他选项卡并关闭计算机时,他们回来时会显示错误消息。 Here is what they see(not this exact image but the same idea):
这是他们看到的内容(不是这个确切的图像,而是相同的想法):
Once the user refreshes the page, it works perfectly again... 用户刷新页面后,它将再次正常运行...
I'm wondering 2 things: 我想知道两件事:
1) is there a way to hide these errors? 1)有没有办法隐藏这些错误?
2) is there a good way to refresh the page when/if they appear? 2)有/当页面出现时刷新页面的好方法吗?
You are using development build. 您正在使用开发版本。 From create-react-app docs (I assume it is create-react-app):
来自create-react-app文档 (我假设它是create-react-app):
When you're ready to deploy to production, create a minified bundle with
npm run build
.准备部署到生产环境时,请使用
npm run build
创建一个缩小的捆绑包。
However uncaught error will cause the whole tree to be unmounted so I suggest to use componentDidCatch hook. 但是,未捕获的错误将导致整个树被卸载,因此我建议使用componentDidCatch挂钩。
You should create an error boundary one level above the App
component and call it something like Wrapper
below. 您应该在
App
组件上方一级创建错误边界 ,并在下面将其命名为Wrapper
。
class Wrapper extends React.Component { componentDidCatch(error, info) { // Do something useful with error like logging to error reporting system // then force reload (if that's what you want): window.location.reload(true); } render() { return (<App/>) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.