如何在nextjs中实现与服务器端渲染反应的错误边界?

时间:2021-01-15 12:27:39

标签: reactjs next.js

ComponentDidCatch(用于实现错误边界)似乎在服务器端不起作用。 这是 React 当前的一个限制,有没有人找到解决这个问题的方法?

1 个答案:

答案 0 :(得分:1)

import React from "react";

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  render() {
    if (this.state.hasError) {
      return "Internal Error.";
    }

    return this.props.children;
  }
}