Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
+93
-41
@@ -1,59 +1,111 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function memoize(fn) {
|
class ErrorBoundary extends React.Component {
|
||||||
const cache = new Map();
|
constructor(props) {
|
||||||
return function(...args) {
|
super(props);
|
||||||
const key = JSON.stringify(args);
|
this.state = {hasError: false};
|
||||||
if (cache.has(key)) {
|
|
||||||
return cache.get(key);
|
|
||||||
}
|
|
||||||
const result = fn.apply(this, args);
|
|
||||||
cache.set(key,result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fibonachi(n, memo={}) {
|
static getDerivedStateFromError(error) {
|
||||||
if (n in memo) {console.log(n);return memo[n];}
|
return { hasError: true }
|
||||||
if (n <= 1) return 1;
|
|
||||||
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
|
||||||
return memo[n];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const MyComponent = ({ value }) => {
|
componentDidCatch(error,info) {
|
||||||
console.log('Rendring');
|
console.log(error,info);
|
||||||
return <div>{value}</div>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const MemoizedComponent = memo(MyComponent);
|
handlerReset = () => {
|
||||||
|
this.setState({hasError: false});
|
||||||
const Child = memo(({ children }) => {
|
}
|
||||||
console.log('Child component rendred');
|
|
||||||
return <div>{children}</div>;
|
|
||||||
});
|
|
||||||
|
|
||||||
const Parent = () => {
|
|
||||||
const [count,setCount] = useState(0);
|
|
||||||
const [text, setText] = useState('');
|
|
||||||
|
|
||||||
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
return (
|
return (
|
||||||
<>
|
<div>
|
||||||
<Child>
|
<h1>Что то поломалось</h1>
|
||||||
{stableChild}
|
<button onClick={this.handlerReset}>Повторить</button>
|
||||||
</Child>
|
</div>)
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
}
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
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 (
|
||||||
|
<div>
|
||||||
|
<h2>Счетчик: {counter}</h2>
|
||||||
|
<button onClick={handlerClick}>Прибавить</button>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Обработка ошибок в компонентах</h1>
|
||||||
<Parent />
|
<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