Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
Generated
+9
-1256
File diff suppressed because it is too large
Load Diff
+1
-4
@@ -4,13 +4,12 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"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
@@ -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>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { StrictMode } from 'react'
|
import { StrictMode } from 'react'
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from 'react-dom/client'
|
||||||
|
// удаляем import './index.css'
|
||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
.container {
|
|
||||||
display:flex;
|
|
||||||
flex-direction: row;
|
|
||||||
border: 1px solid black;
|
|
||||||
border-radius: 5px;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
p {
|
|
||||||
padding: 5px 10px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.counter {
|
|
||||||
&_display{
|
|
||||||
border: 1px solid black;
|
|
||||||
border-width: 3px;
|
|
||||||
border-radius: 10px;
|
|
||||||
padding: 10px;
|
|
||||||
width: 300px;
|
|
||||||
}
|
|
||||||
&_button{
|
|
||||||
padding: 10px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user