React Error boundary does
Trying to understand why throwing Error does not work with Error Boundary
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
class Boundary extends React.Component {
constructor() {
super();
this.state = {}
}
componentDidCatch(error, info) {
this.setState({
error,
info,
})
}
render() {
if (this.state.error) {
return (
<div>
{JSON.stringify(this.state)}<br/><br/>
Error message: "{this.state.error.message}"
</div>)
}
return this.props.children;
}
}
class Component extends React.Component {
render() {
// Why and what should i do ?
//throw 'This error will be available'
throw new Error('This will not be available');
}
}
class TodoApp extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<Boundary>
<div>
<Component />
</div>
</Boundary>
)
}
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))