Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
+90
-37
@@ -1,58 +1,111 @@
|
||||
import React from 'react'
|
||||
|
||||
class RandomUser extends React.Component {
|
||||
class ErrorBoundary extends React.Component {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.state = { user: {} }
|
||||
super(props);
|
||||
this.state = {hasError: false};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://randomuser.me/api')
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({ user: json.results[0] }));
|
||||
console.log('смонтирован')
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasError: true }
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps,prevState) {
|
||||
console.log(this.state);
|
||||
console.log('Компонент обнвлен')
|
||||
componentDidCatch(error,info) {
|
||||
console.log(error,info);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
console.log('Компонент таймера размонтирован')
|
||||
handlerReset = () => {
|
||||
this.setState({hasError: false});
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div>
|
||||
{this.state.user &&
|
||||
this.state.user.name &&
|
||||
(
|
||||
<>
|
||||
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
||||
<img src={this.state.user.picture.medium} />
|
||||
<button onClick={() => {
|
||||
console.log('Загружаем ... ')
|
||||
fetch('https://randomuser.me/api')
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({ user: json.results[0] }));
|
||||
}}>Загрузить нового</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
</div>
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div>
|
||||
<h1>Что то поломалось</h1>
|
||||
<button onClick={this.handlerReset}>Повторить</button>
|
||||
</div>)
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
function App() {
|
||||
const [showClock, setShowClock] = React.useState(true);
|
||||
class AdvancedErrorBoundary extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
hasError: false,
|
||||
error: null,
|
||||
errorInfo: null
|
||||
}
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasErorr: true }
|
||||
}
|
||||
|
||||
componentDidCatch(error, info) {
|
||||
this.setState({
|
||||
error: error,
|
||||
errorInfo: info
|
||||
})
|
||||
}
|
||||
|
||||
handleReset = () => {
|
||||
this.setState({
|
||||
hasError:false,
|
||||
error: null,
|
||||
errorInfo:null
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
if(this.state.hasError) {
|
||||
console.log('Error');
|
||||
return (
|
||||
<div>
|
||||
<h2>Произошла ошибка</h2>
|
||||
<p>Извеняемся, работаем, исправляем. Разработчика привязали к батарее</p>
|
||||
<button onClick={this.handleReset}>Попробовать снова</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
//Если ошибок нет, то рендрим дочерние объекты
|
||||
return this.props.children
|
||||
}
|
||||
}
|
||||
|
||||
function ProblemComponent() {
|
||||
const [counter,setCounter] = React.useState(0)
|
||||
|
||||
const handlerClick = () => setCounter(counter + 1)
|
||||
|
||||
React.useEffect(() => {
|
||||
if (counter > 5) {
|
||||
throw new Error("Ошибка в компоненте");
|
||||
}
|
||||
},[counter]);
|
||||
return (
|
||||
<div>
|
||||
<h2>Счетчик: {counter}</h2>
|
||||
<button onClick={handlerClick}>Прибавить</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<>
|
||||
<h1>Домашнее задание: жизненый цикл</h1>
|
||||
<button onClick={()=> setShowClock(!showClock)}>
|
||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||
</button>
|
||||
{showClock && <RandomUser />}
|
||||
<h1>Обработка ошибок в компонентах</h1>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user