Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 10e56d8aaa |
+69
-98
@@ -1,111 +1,82 @@
|
|||||||
import React from 'react'
|
import memo, { useMemo, useCallback, useState } from 'react'
|
||||||
|
|
||||||
class ErrorBoundary extends React.Component {
|
function MyComponent() {
|
||||||
constructor(props) {
|
const [count, setCount] = useState(0);
|
||||||
super(props);
|
const [name, setName] = useState('John');
|
||||||
this.state = {hasError: false};
|
|
||||||
}
|
|
||||||
|
|
||||||
static getDerivedStateFromError(error) {
|
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
||||||
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 {
|
|
||||||
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 (
|
return (
|
||||||
<div>
|
<>
|
||||||
<h2>Счетчик: {counter}</h2>
|
<h2>{greeting}</h2>
|
||||||
<button onClick={handlerClick}>Прибавить</button>
|
<button onClick={()=>setName('Alex')}>New name</button>
|
||||||
</div>
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function MyComponent2() {
|
||||||
|
const [data,setData] = useState([{theme:'light'}])
|
||||||
|
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
||||||
|
|
||||||
|
const processData = useMemo(()=>{
|
||||||
|
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
||||||
|
},[data, userPreferences]);
|
||||||
|
console.log(processData);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h2 className={data.theme}>2</h2>
|
||||||
|
<button onClick={()=>setData(data)}>set new theme</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function MyComponent3() {
|
||||||
|
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
||||||
|
|
||||||
|
//Применение оправдано
|
||||||
|
const complexResult = useMemo(()=>{
|
||||||
|
return heavyMethOperations(largeDataSet);
|
||||||
|
},[largeDataSet])
|
||||||
|
}
|
||||||
|
|
||||||
|
function ParentComponent({ items }) {
|
||||||
|
const [filter, setFilter] = useState('');
|
||||||
|
|
||||||
|
const listProps = useMemo(()=>({
|
||||||
|
items: items.filter(item => item.name.includes(filter)),
|
||||||
|
onItemClick: id => console.log('Clicked',id),
|
||||||
|
theme: 'dark'
|
||||||
|
}),[items,filter]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Memoized {...listProps} />
|
||||||
|
<button onClick={()=>setFilter('Bob')}>Click</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Memoized(props) {
|
||||||
|
console.log(props);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h3 className={props.theme}>Memoized</h3>
|
||||||
|
{props.items.map((item) => (
|
||||||
|
<>
|
||||||
|
<li>
|
||||||
|
{item.name}
|
||||||
|
</li>
|
||||||
|
</>))}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Обработка ошибок в компонентах</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<ErrorBoundary>
|
<h1>Hello, React!</h1>
|
||||||
<ProblemComponent />
|
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
||||||
</ErrorBoundary>
|
|
||||||
<ErrorBoundary>
|
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
<ErrorBoundary>
|
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,5 @@ 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