Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
+41
-93
@@ -1,112 +1,60 @@
|
||||
import React from 'react'
|
||||
import { memo, useState, useMemo } from 'react'
|
||||
|
||||
class ErrorBoundary extends React.Component {
|
||||
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>)
|
||||
function memoize(fn) {
|
||||
const cache = new Map();
|
||||
return function(...args) {
|
||||
const key = JSON.stringify(args);
|
||||
if (cache.has(key)) {
|
||||
return cache.get(key);
|
||||
}
|
||||
return this.props.children;
|
||||
const result = fn.apply(this, args);
|
||||
cache.set(key,result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
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 fibonachi(n, memo={}) {
|
||||
if (n in memo) {console.log(n);return memo[n];}
|
||||
if (n <= 1) return 1;
|
||||
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
||||
return memo[n];
|
||||
}
|
||||
|
||||
function ProblemComponent() {
|
||||
const [counter,setCounter] = React.useState(0)
|
||||
const MyComponent = ({ value }) => {
|
||||
console.log('Rendring');
|
||||
return <div>{value}</div>
|
||||
}
|
||||
|
||||
const MemoizedComponent = memo(MyComponent);
|
||||
|
||||
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 handlerClick = () => setCounter(counter + 1)
|
||||
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (counter > 5) {
|
||||
throw new Error("Ошибка в компоненте");
|
||||
}
|
||||
},[counter]);
|
||||
return (
|
||||
<div>
|
||||
<h2>Счетчик: {counter}</h2>
|
||||
<button onClick={handlerClick}>Прибавить</button>
|
||||
</div>
|
||||
<>
|
||||
<Child>
|
||||
{stableChild}
|
||||
</Child>
|
||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<>
|
||||
<h1>Обработка ошибок в компонентах</h1>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
</>
|
||||
<h1>Hello, React!</h1>
|
||||
<Parent />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user