在React中捕获全局错误

时间:2018-04-04 10:18:06

标签: reactjs

如果有人知道,那么关于错误处理的一般反应问题。出于原因,我试图做以下

try {
    ReactDOM.render(<App />, document.getElementById("root"));
} catch (error) {
    console.log("CAUGHT AN ERROR!!!!!!!")
    console.log(error);
   //send the entire redux state to the server and notify some services, then rethrow
    throw error;
}

然后我意识到这不起作用,我想知道为什么。

通过“它没有”工作我的意思是,错误一直冒泡到index.js,这是包含ReactDOM.render代码的文件,但它从来没有真正被我的捕获。

如果有人能解释我的原因,我将不胜感激。

编辑:在React中有这个名为https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html的错误边界,但是这似乎只有在从Render抛出错误时才有效。我的错误是在不同的Component类方法中引发的

0 个答案:

没有答案