Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0597891e1 |
Generated
-27
@@ -8,7 +8,6 @@
|
|||||||
"name": "store-client",
|
"name": "store-client",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/react-query": "^5.100.14",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -1305,32 +1304,6 @@
|
|||||||
"win32"
|
"win32"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"node_modules/@tanstack/query-core": {
|
|
||||||
"version": "5.100.14",
|
|
||||||
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.100.14.tgz",
|
|
||||||
"integrity": "sha512-5X41dGpxgeaHISCRW2oYwcSycZeULZzAunaudXT9ov1KOTj9xwt0CH6hbwqP1/z74ZWF7rYFnDpyYH07XFcZew==",
|
|
||||||
"license": "MIT",
|
|
||||||
"funding": {
|
|
||||||
"type": "github",
|
|
||||||
"url": "https://github.com/sponsors/tannerlinsley"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@tanstack/react-query": {
|
|
||||||
"version": "5.100.14",
|
|
||||||
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.100.14.tgz",
|
|
||||||
"integrity": "sha512-oOr6aRdSFEwWhzxEkD/9ZcItM3+LjBSkeVmadWKwUssAHTsqd/7bOjWrX4AbvEkoEhgAxzN0Xk6H/aYzXiYBAw==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"@tanstack/query-core": "5.100.14"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "github",
|
|
||||||
"url": "https://github.com/sponsors/tannerlinsley"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"react": "^18 || ^19"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@types/babel__core": {
|
"node_modules/@types/babel__core": {
|
||||||
"version": "7.20.5",
|
"version": "7.20.5",
|
||||||
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",
|
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/react-query": "^5.100.14",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+93
-75
@@ -1,93 +1,111 @@
|
|||||||
import React, {useState, useEffect, useRef } from 'react'
|
import React from 'react'
|
||||||
//import { useQuery } from '@tanstack/react-query'
|
|
||||||
|
|
||||||
function useWindowWidth() {
|
class ErrorBoundary extends React.Component {
|
||||||
const [width,setWidth] = useState(window.innerWidth)
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
useEffect(() => {
|
this.state = {hasError: false};
|
||||||
const handleResize = () => setWidth(window.innerWidth);
|
|
||||||
window.addEventListener('resize',handleResize);
|
|
||||||
return () => window.removeEventListener('resize',handleResize);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return width
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function useLocalStorage(key, initValue) {
|
static getDerivedStateFromError(error) {
|
||||||
const [value, setValue] = useState(()=>{
|
return { hasError: true }
|
||||||
const stored = localStorage.getItem(key);
|
}
|
||||||
return stored !== null ? JSON.parse(stored) : initValue
|
|
||||||
|
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
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem(key, JSON.stringify(value))
|
|
||||||
},[key, value])
|
|
||||||
|
|
||||||
return [value,setValue]
|
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
function useUserData(userId) {
|
handleReset = () => {
|
||||||
return useQuery(
|
this.setState({
|
||||||
['user',userId],
|
hasError:false,
|
||||||
() => fetch(`/api/user/${userId}`).then(res => res.json()),
|
error: null,
|
||||||
{refetchOnWindowFocus: false}
|
errorInfo:null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if(this.state.hasError) {
|
||||||
|
console.log('Error');
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h2>Произошла ошибка</h2>
|
||||||
|
<p>Извеняемся, работаем, исправляем. Разработчика привязали к батарее</p>
|
||||||
|
<button onClick={this.handleReset}>Попробовать снова</button>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}*/
|
}
|
||||||
|
//Если ошибок нет, то рендрим дочерние объекты
|
||||||
function useInput(initValue, validate) {
|
return this.props.children
|
||||||
const [value, setValue] = useState(initValue);
|
|
||||||
const [error, setError] = useState(null);
|
|
||||||
|
|
||||||
const onChange = e => {
|
|
||||||
const newValue = e.target.value
|
|
||||||
setValue(newValue)
|
|
||||||
if (validate) {
|
|
||||||
const validationResult = validate(newValue)
|
|
||||||
setError(validationResult)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { value, error, onChange }
|
function ProblemComponent() {
|
||||||
|
const [counter,setCounter] = React.useState(0)
|
||||||
|
|
||||||
|
const handlerClick = () => setCounter(counter + 1)
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (counter > 5) {
|
||||||
|
throw new Error("Ошибка в компоненте");
|
||||||
}
|
}
|
||||||
|
},[counter]);
|
||||||
function useInterval(callback, delay) {
|
return (
|
||||||
const savedCallback = useRef()
|
<div>
|
||||||
|
<h2>Счетчик: {counter}</h2>
|
||||||
useEffect(()=>{
|
<button onClick={handlerClick}>Прибавить</button>
|
||||||
savedCallback.current = callback
|
</div>
|
||||||
},[callback]);
|
)
|
||||||
|
|
||||||
useEffect(()=>{
|
|
||||||
if (delay == null) return
|
|
||||||
const id = setInterval(()=>{
|
|
||||||
savedCallback.current()
|
|
||||||
},delay);
|
|
||||||
return () => clearInterval(id);
|
|
||||||
},[delay])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const width = useWindowWidth();
|
|
||||||
const [theme, setTheme] = useLocalStorage('theme','light');
|
|
||||||
|
|
||||||
const username = useInput(
|
|
||||||
'',
|
|
||||||
value => value.length < 3 ? 'минимум 3 символа' : null
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!{width}</h1>
|
<h1>Обработка ошибок в компонентах</h1>
|
||||||
<button onClick={()=>{
|
<ErrorBoundary>
|
||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
<ProblemComponent />
|
||||||
}}>
|
</ErrorBoundary>
|
||||||
Сменить тему
|
<ErrorBoundary>
|
||||||
</button>
|
<ProblemComponent />
|
||||||
<p><input type="text"
|
</ErrorBoundary>
|
||||||
value={username.value}
|
<ErrorBoundary>
|
||||||
onChange={username.onChange} />
|
<ProblemComponent />
|
||||||
{ username.error ? username.error : '' }
|
</ErrorBoundary>
|
||||||
</p>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user