Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as e0597891e1 ErrorBoundary 2026-05-28 21:41:57 +03:00
3 changed files with 111 additions and 1279 deletions
+9 -1256
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -10,7 +10,6 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"info": "^1.0.0",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0"
}, },
@@ -23,8 +22,6 @@
"eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19", "eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0", "globals": "^16.0.0",
"sass": "^1.99.0",
"sass-embedded": "^1.99.0",
"vite": "^6.3.5" "vite": "^6.3.5"
} }
} }
+101 -19
View File
@@ -1,29 +1,111 @@
import styles from './styles/App.module.scss' import React from 'react'
import React, { useState, useCallback } from 'react'
function CounterButton({label, onClick}) { class ErrorBoundary extends React.Component {
return ( constructor(props) {
<button onClick={onClick} className={styles.counter_button}>{label}</button> super(props);
) this.state = {hasError: false};
}
static getDerivedStateFromError(error) {
return { hasError: true }
}
componentDidCatch(error,info) {
console.log(error,info);
}
handlerReset = () => {
this.setState({hasError: false});
}
render() {
if (this.state.hasError) {
return (
<div>
<h1>Что то поломалось</h1>
<button onClick={this.handlerReset}>Повторить</button>
</div>)
}
return this.props.children;
}
} }
function CounterDisplay({value}) {
return (<div className={styles.counter_display}> class AdvancedErrorBoundary extends React.Component {
Значение счетчика: <b>{value}</b> constructor(props) {
</div>) 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() { function App() {
const [counter,setCounter] = useState(0);
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
return ( return (
<> <>
<h2>Практика по созданию счетчика с колбэками</h2> <h1>Обработка ошибок в компонентах</h1>
<CounterButton label="Увеличить" onClick={increment} /> <ErrorBoundary>
<CounterButton label="уменьшить" onClick={decrement} /> <ProblemComponent />
<CounterDisplay value={counter} /> </ErrorBoundary>
<ErrorBoundary>
<ProblemComponent />
</ErrorBoundary>
<ErrorBoundary>
<ProblemComponent />
</ErrorBoundary>
</> </>
) )
} }