errorboundary
This commit is contained in:
parent
c664806c7b
commit
c7b46dadc3
24
classes/src/components/ErrorBoundary.js
Normal file
24
classes/src/components/ErrorBoundary.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { Component } from "react";
|
||||||
|
|
||||||
|
class ErrorBoundary extends Component {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.state = {
|
||||||
|
hasError: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidCatch(error) {
|
||||||
|
console.log(error)
|
||||||
|
this.setState({ hasError: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
return <p>Something went wrong</p>;
|
||||||
|
}
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ErrorBoundary;
|
@ -1,5 +1,6 @@
|
|||||||
import { Component, Fragment } from "react";
|
import { Component, Fragment } from "react";
|
||||||
import UsersContext from "../store/users-context";
|
import UsersContext from "../store/users-context";
|
||||||
|
import ErrorBoundary from "./ErrorBoundary";
|
||||||
import classes from "./UserFinder.module.css";
|
import classes from "./UserFinder.module.css";
|
||||||
import Users from "./Users";
|
import Users from "./Users";
|
||||||
|
|
||||||
@ -35,10 +36,15 @@ class UserFinder extends Component {
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
|
<ErrorBoundary>
|
||||||
<div className={classes.finder}>
|
<div className={classes.finder}>
|
||||||
<input type="search" onChange={this.searchChangeHandler.bind(this)} />
|
<input
|
||||||
|
type="search"
|
||||||
|
onChange={this.searchChangeHandler.bind(this)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Users users={this.state.filteredUsers} />
|
<Users users={this.state.filteredUsers} />
|
||||||
|
</ErrorBoundary>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,12 @@ class Users extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(){
|
||||||
|
if (this.props.users.length === 0) {
|
||||||
|
throw new Error('no users')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toggleUsersHandler() {
|
toggleUsersHandler() {
|
||||||
this.setState((curState) => {
|
this.setState((curState) => {
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user