React ErrorBoundary不适用于Meteor SSR

时间:2018-06-18 07:45:48

标签: javascript reactjs meteor meteor-react

React ErrorBoundary不适用于使用Meteor的服务器端渲染

我的App.js(这是我抛出错误的地方)

class App extends Component {
  render() {
    // here
    throw new Error("trol");
    return (
      <div className="App">
        <header className="App-header">
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

我的ErrorBoundary.js

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

    componentDidCatch(error, info) {
        // Display fallback UI
        this.setState({ hasError: true });
        // You can also log the error to an error reporting service
        //logErrorToMyService(error, info);
    }

    render() {
        if (this.state.hasError) {
            // You can render any custom fallback UI
            return <h1>Something went wrong.</h1>;
        } else {
            return this.props.children;
        }

    }
}

我的client/main.js

export const renderRoutes = () => (
    <ErrorBoundary>
      <App />
    </ErrorBoundary>
);

Meteor.startup(() => {
  render(renderRoutes() , document.getElementById('render-target'));
});

我的server/main.js

onPageLoad(sink => {
  const html = "<div id='render-target'>" + renderToString(
    <ErrorBoundary>
      <App />
    </ErrorBoundary>
  ) + "</div>";
  sink.appendToBody(html);
});


Meteor.startup(() => {
  // code to run on server at startup
});

如果我从onPageLoad中移除server/main.js函数,它将仅在客户端呈现,并且它将起作用。否则,页面将无法加载,我将在服务器控制台中出现以下错误

Error running template: Error: trol

如果我使用Meteor.Error会发生同样的事情。

0 个答案:

没有答案