Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
+91
-62
@@ -1,82 +1,111 @@
|
|||||||
import memo, { useMemo, useCallback, useState } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function MyComponent() {
|
class ErrorBoundary extends React.Component {
|
||||||
const [count, setCount] = useState(0);
|
constructor(props) {
|
||||||
const [name, setName] = useState('John');
|
super(props);
|
||||||
|
this.state = {hasError: false};
|
||||||
|
}
|
||||||
|
|
||||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
static getDerivedStateFromError(error) {
|
||||||
|
return { hasError: true }
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
componentDidCatch(error,info) {
|
||||||
<>
|
console.log(error,info);
|
||||||
<h2>{greeting}</h2>
|
}
|
||||||
<button onClick={()=>setName('Alex')}>New name</button>
|
|
||||||
</>
|
handlerReset = () => {
|
||||||
)
|
this.setState({hasError: false});
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Что то поломалось</h1>
|
||||||
|
<button onClick={this.handlerReset}>Повторить</button>
|
||||||
|
</div>)
|
||||||
|
}
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function MyComponent2() {
|
class AdvancedErrorBoundary extends React.Component {
|
||||||
const [data,setData] = useState([{theme:'light'}])
|
constructor(props) {
|
||||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
hasError: false,
|
||||||
|
error: null,
|
||||||
|
errorInfo: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const processData = useMemo(()=>{
|
static getDerivedStateFromError(error) {
|
||||||
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
return { hasErorr: true }
|
||||||
},[data, userPreferences]);
|
}
|
||||||
console.log(processData);
|
|
||||||
return (
|
componentDidCatch(error, info) {
|
||||||
<>
|
this.setState({
|
||||||
<h2 className={data.theme}>2</h2>
|
error: error,
|
||||||
<button onClick={()=>setData(data)}>set new theme</button>
|
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 MyComponent3() {
|
function ProblemComponent() {
|
||||||
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
const [counter,setCounter] = React.useState(0)
|
||||||
|
|
||||||
//Применение оправдано
|
const handlerClick = () => setCounter(counter + 1)
|
||||||
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]);
|
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (counter > 5) {
|
||||||
|
throw new Error("Ошибка в компоненте");
|
||||||
|
}
|
||||||
|
},[counter]);
|
||||||
return (
|
return (
|
||||||
<>
|
<div>
|
||||||
<Memoized {...listProps} />
|
<h2>Счетчик: {counter}</h2>
|
||||||
<button onClick={()=>setFilter('Bob')}>Click</button>
|
<button onClick={handlerClick}>Прибавить</button>
|
||||||
</>
|
</div>
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
||||||
<h1>Hello, React!</h1>
|
<ErrorBoundary>
|
||||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
<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