As per the new react 16 release doc it says
"React 16 prints all errors that occurred during rendering to the console in development, even if the application accidentally swallows them."
I have a Parent component and a Child component. I have triggered an error in then block of promise. But it will call catch method of the promise, componentDidCatch of parent is not getting called. I am not sure whether this the expected behaviour.
Here is the jsfiddle https://jsfiddle.net/john1jan/Luktwrdm/14/
class Parent extends React.Component {
constructor(props){
super(props);
this.state={hasError:false}
}
render() {
if(this.state.hasError){
return <div>Something went wrong</div>
}
return (
<div>
<h1>I am parent</h1>
<Child></Child>
</div>
);
}
componentDidCatch(error,errorInfo){
this.setState({
hasError:true
})
}
}
class Child extends React.Component {
render() {
return (
<div>
<h1>I am child</h1>
</div>
);
}
componentDidMount(){
fetch("https://jsonplaceholder.typicode.com/posts").then((response) => {
throw new Error("I am the error in promise");
}).catch((error)=>{
console.log("Error caught in catch",error);
})
;
}
}
ReactDOM.render(<Parent name="world"/>,
document.getElementById('container'));