Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
+88
-35
@@ -1,58 +1,111 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
class ErrorBoundary extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props);
|
||||||
this.state = { user: {} }
|
this.state = {hasError: false};
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
static getDerivedStateFromError(error) {
|
||||||
fetch('https://randomuser.me/api')
|
return { hasError: true }
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
|
||||||
console.log('смонтирован')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps,prevState) {
|
componentDidCatch(error,info) {
|
||||||
console.log(this.state);
|
console.log(error,info);
|
||||||
console.log('Компонент обнвлен')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
handlerReset = () => {
|
||||||
console.log('Компонент таймера размонтирован')
|
this.setState({hasError: false});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div>
|
if (this.state.hasError) {
|
||||||
{this.state.user &&
|
return (
|
||||||
this.state.user.name &&
|
<div>
|
||||||
(
|
<h1>Что то поломалось</h1>
|
||||||
<>
|
<button onClick={this.handlerReset}>Повторить</button>
|
||||||
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
</div>)
|
||||||
<img src={this.state.user.picture.medium} />
|
}
|
||||||
<button onClick={() => {
|
return this.props.children;
|
||||||
console.log('Загружаем ... ')
|
}
|
||||||
fetch('https://randomuser.me/api')
|
}
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
class AdvancedErrorBoundary extends React.Component {
|
||||||
}}>Загрузить нового</button>
|
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>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
</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 [showClock, setShowClock] = React.useState(true);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Домашнее задание: жизненый цикл</h1>
|
<h1>Обработка ошибок в компонентах</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<ErrorBoundary>
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<ProblemComponent />
|
||||||
</button>
|
</ErrorBoundary>
|
||||||
{showClock && <RandomUser />}
|
<ErrorBoundary>
|
||||||
|
<ProblemComponent />
|
||||||
|
</ErrorBoundary>
|
||||||
|
<ErrorBoundary>
|
||||||
|
<ProblemComponent />
|
||||||
|
</ErrorBoundary>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
<StrictMode>
|
||||||
<App />
|
<App />
|
||||||
|
</StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user