Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ee1bf2eee | |||
| c137c644e3 |
+22
-89
@@ -1,111 +1,44 @@
|
||||
import React from 'react'
|
||||
|
||||
class ErrorBoundary extends React.Component {
|
||||
class Clock extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {hasError: false};
|
||||
super(props)
|
||||
this.state = { time: new Date().toLocaleTimeString() }
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasError: true }
|
||||
componentDidMount() {
|
||||
this.timerId = setInterval(()=>{
|
||||
this.setState({ time: new Date().toLocaleTimeString() })
|
||||
}, 1000);
|
||||
console.log('Таймер смонтирован')
|
||||
}
|
||||
|
||||
componentDidCatch(error,info) {
|
||||
console.log(error,info);
|
||||
componentDidUpdate(prevProps,prevState) {
|
||||
console.log('Компонент обнвлен')
|
||||
}
|
||||
|
||||
handlerReset = () => {
|
||||
this.setState({hasError: false});
|
||||
componentWillUnmount() {
|
||||
clearInterval(this.timerId)
|
||||
console.log('Компонент таймера размонтирован')
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div>
|
||||
<h1>Что то поломалось</h1>
|
||||
<button onClick={this.handlerReset}>Повторить</button>
|
||||
</div>)
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
return <div>
|
||||
<h2>Текущее время: {this.state.time}</h2>
|
||||
</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() {
|
||||
const [showClock, setShowClock] = React.useState(true);
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Обработка ошибок в компонентах</h1>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<h1>Жизненный цикл компонента часов</h1>
|
||||
<button onClick={()=> setShowClock(!showClock)}>
|
||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||
</button>
|
||||
{showClock && <Clock />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,5 @@ 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