Compare commits

..

7 Commits

3 changed files with 1278 additions and 110 deletions
+1256 -9
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -10,6 +10,7 @@
"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"
}, },
@@ -22,6 +23,8 @@
"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"
} }
} }
+18 -100
View File
@@ -1,111 +1,29 @@
import React from 'react' import styles from './styles/App.module.scss'
import React, { useState, useCallback } from 'react'
class ErrorBoundary extends React.Component { function CounterButton({label, onClick}) {
constructor(props) {
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;
}
}
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 ( return (
<div> <button onClick={onClick} className={styles.counter_button}>{label}</button>
<h2>Счетчик: {counter}</h2>
<button onClick={handlerClick}>Прибавить</button>
</div>
) )
} }
function CounterDisplay({value}) {
return (<div className={styles.counter_display}>
Значение счетчика: <b>{value}</b>
</div>)
}
function App() { function App() {
const [counter,setCounter] = useState(0);
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
return ( return (
<> <>
<h1>Обработка ошибок в компонентах</h1> <h2>Практика по созданию счетчика с колбэками</h2>
<ErrorBoundary> <CounterButton label="Увеличить" onClick={increment} />
<ProblemComponent /> <CounterButton label="уменьшить" onClick={decrement} />
</ErrorBoundary> <CounterDisplay value={counter} />
<ErrorBoundary>
<ProblemComponent />
</ErrorBoundary>
<ErrorBoundary>
<ProblemComponent />
</ErrorBoundary>
</> </>
) )
} }