Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
+64
-97
@@ -1,111 +1,78 @@
|
|||||||
import React from 'react'
|
import { useState, createContext, useContext } from 'react'
|
||||||
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
class ErrorBoundary extends React.Component {
|
const UserContext = createContext('')
|
||||||
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 {
|
function UserProvider({ children }) {
|
||||||
constructor(props) {
|
const [name,setName] = useState('');
|
||||||
super(props);
|
|
||||||
this.state = {
|
const toggleName = (newName) => {
|
||||||
hasError: false,
|
setName(newName);
|
||||||
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>
|
<UserContext.Provider value={{name, toggleName}}>
|
||||||
<h2>Счетчик: {counter}</h2>
|
{children}
|
||||||
<button onClick={handlerClick}>Прибавить</button>
|
</UserContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Form() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
const [newName,setNewName] = useState('');
|
||||||
|
const handleSubmit = (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
toggleName(event.target[0].value)
|
||||||
|
}
|
||||||
|
const handlerChange = (event) => {
|
||||||
|
setNewName(event.target.value);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type='text' value={newName} onChange={handlerChange} />
|
||||||
|
<button type='submit'>Войти</button>
|
||||||
|
</form>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ExitButton() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button onClick={() => toggleName('')}>Выход</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Toolbar() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
return(
|
||||||
|
<div className={styles.container}>
|
||||||
|
<h3>Панель пользователя</h3>
|
||||||
|
{name && `Привет ${name}`}
|
||||||
|
{name ? <ExitButton /> : <Form /> }
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function Page() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||||
|
<Toolbar />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Обработка ошибок в компонентах</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<ErrorBoundary>
|
<h1>Hello, React!</h1>
|
||||||
<ProblemComponent />
|
<UserProvider>
|
||||||
</ErrorBoundary>
|
<Page />
|
||||||
<ErrorBoundary>
|
</UserProvider>
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
<ErrorBoundary>
|
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
.container {
|
||||||
|
height: 500px;
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 10px;
|
||||||
|
background-color: lightblue;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user