Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
+89
-76
@@ -1,98 +1,111 @@
|
|||||||
import { useState, useRef, useEffect } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function Example() {
|
class ErrorBoundary extends React.Component {
|
||||||
const myRef = useRef(null);
|
constructor(props) {
|
||||||
const counterRef = useRef(0);
|
super(props);
|
||||||
const [text, setText] = useState('');
|
this.state = {hasError: false};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
myRef.current?.focus();
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
const handlerFocus = () => {
|
|
||||||
if(myRef.current) {
|
|
||||||
myRef.current.focus();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlerChange = (event) => {
|
|
||||||
setText(event.target.value);
|
|
||||||
}
|
}
|
||||||
counterRef.current += 1;
|
|
||||||
return <div>
|
|
||||||
<input ref={myRef} text="text" /><br />
|
|
||||||
Количество рендров: {counterRef.current}<br />
|
|
||||||
<input text="text" onChange={handlerChange} value={text} />
|
|
||||||
<button onClick={handlerFocus}>Поставить фокус на поле</button>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
function ClickCounter() {
|
static getDerivedStateFromError(error) {
|
||||||
const countRef = useRef(0);
|
return { hasError: true }
|
||||||
|
|
||||||
function handlerClick() {
|
|
||||||
countRef.current += 1;
|
|
||||||
//alert(`Нажали ${countRef.current} раз.`);
|
|
||||||
}
|
}
|
||||||
return <button onClick={handlerClick}>Нажать</button>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function TimerComponent() {
|
componentDidCatch(error,info) {
|
||||||
const timerIdRef = useRef(null);
|
console.log(error,info);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
handlerReset = () => {
|
||||||
timerIdRef.current = setTimeout(() => {
|
this.setState({hasError: false});
|
||||||
alert("Прошло 3 секунды");
|
}
|
||||||
}, 3000);
|
|
||||||
return () => {
|
render() {
|
||||||
clearTimeout(timerIdRef.current);
|
if (this.state.hasError) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Что то поломалось</h1>
|
||||||
|
<button onClick={this.handlerReset}>Повторить</button>
|
||||||
|
</div>)
|
||||||
}
|
}
|
||||||
},[]);
|
return this.props.children;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function PrevValue({ value }) {
|
class AdvancedErrorBoundary extends React.Component {
|
||||||
const prevValueRef = useRef();
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
hasError: false,
|
||||||
|
error: null,
|
||||||
|
errorInfo: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
static getDerivedStateFromError(error) {
|
||||||
prevValueRef.current = value;
|
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>
|
<div>
|
||||||
Текущее значение: {value} <br />
|
<h2>Счетчик: {counter}</h2>
|
||||||
Предыдущее значение: {prevValueRef.current}
|
<button onClick={handlerClick}>Прибавить</button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function ScrollList() {
|
|
||||||
const listRef = useRef(null);
|
|
||||||
|
|
||||||
const scrollToBottom = () => {
|
|
||||||
if(listRef.current) {
|
|
||||||
listRef.current.scrollTop = listRef.current.scrollHeight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div ref={listRef}
|
|
||||||
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
|
||||||
{[...Array(20)].map((_,i)=>(
|
|
||||||
<div key={i}>Элемент № {i + 1}</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Обработка ошибок в компонентах</h1>
|
||||||
<ScrollList />
|
<ErrorBoundary>
|
||||||
|
<ProblemComponent />
|
||||||
|
</ErrorBoundary>
|
||||||
|
<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