Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aeb40b20 |
+30
-97
@@ -1,111 +1,44 @@
|
|||||||
import React from 'react'
|
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
||||||
|
import PostAddForm from './components/PostAddForm'
|
||||||
|
|
||||||
class ErrorBoundary extends React.Component {
|
/*
|
||||||
constructor(props) {
|
const mutation = useMutation({
|
||||||
super(props);
|
mutationFn: newUser => {
|
||||||
this.state = {hasError: false};
|
fetch('https://jsonplaceholder.typicode.com/users', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {'Content-type':'application/json'},
|
||||||
|
body: JSON.stringify(newUser),
|
||||||
|
}).then(res => res.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
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>)
|
|
||||||
}
|
|
||||||
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 = () => {
|
const createQueryClient = () => {
|
||||||
this.setState({
|
new QueryClient({
|
||||||
hasError:false,
|
defaultOptions: {
|
||||||
error: null,
|
queries: {
|
||||||
errorInfo:null
|
staleTime: 5 * 60*1000,
|
||||||
});
|
retry: 1,
|
||||||
}
|
gcTime: 10*60*1000,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
render() {
|
},
|
||||||
if(this.state.hasError) {
|
mutations: {
|
||||||
console.log('Error');
|
retry:0,
|
||||||
return (
|
onError: error => {console.error('Mutation error:',error.message)}
|
||||||
<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>Обработка ошибок в компонентах</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<ErrorBoundary>
|
<h1>Hello, React!</h1>
|
||||||
<ProblemComponent />
|
<QueryClientProvider client={createQueryClient}>
|
||||||
</ErrorBoundary>
|
<PostAddForm />
|
||||||
<ErrorBoundary>
|
</QueryClientProvider>
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
<ErrorBoundary>
|
|
||||||
<ProblemComponent />
|
|
||||||
</ErrorBoundary>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user