From 35431f57d988558b4149a847a308b0164fd41107 Mon Sep 17 00:00:00 2001 From: Laktionov Anton Date: Tue, 2 Jun 2026 21:22:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D0=BE=D0=BC=D0=BD=D1=8B=D1=85=20=D1=85=D1=83?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 27 +++++++++++++++++ package.json | 3 +- src/App.jsx | 74 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b4904ee..deda1e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "store-client", "version": "0.0.0", "dependencies": { + "@tanstack/react-query": "^5.100.14", "react": "^19.1.0", "react-dom": "^19.1.0" }, @@ -1304,6 +1305,32 @@ "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": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", diff --git a/package.json b/package.json index 151176e..936b80f 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,13 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite", + "dev": "vite", "build": "vite build", "lint": "eslint .", "preview": "vite preview" }, "dependencies": { + "@tanstack/react-query": "^5.100.14", "react": "^19.1.0", "react-dom": "^19.1.0" }, diff --git a/src/App.jsx b/src/App.jsx index 974f9d7..487ca79 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,4 +1,5 @@ -import React, {useState, useEffect} from 'react' +import React, {useState, useEffect, useRef } from 'react' +//import { useQuery } from '@tanstack/react-query' function useWindowWidth() { const [width,setWidth] = useState(window.innerWidth) @@ -12,12 +13,81 @@ function useWindowWidth() { return width } +function useLocalStorage(key, initValue) { + const [value, setValue] = useState(()=>{ + const stored = localStorage.getItem(key); + return stored !== null ? JSON.parse(stored) : initValue + }) + + useEffect(() => { + localStorage.setItem(key, JSON.stringify(value)) + },[key, value]) + + return [value,setValue] +} +/* +function useUserData(userId) { + return useQuery( + ['user',userId], + () => fetch(`/api/user/${userId}`).then(res => res.json()), + {refetchOnWindowFocus: false} + ) +}*/ + +function useInput(initValue, validate) { + 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 useInterval(callback, delay) { + const savedCallback = useRef() + + useEffect(()=>{ + savedCallback.current = callback + },[callback]); + + useEffect(()=>{ + if (delay == null) return + const id = setInterval(()=>{ + savedCallback.current() + },delay); + return () => clearInterval(id); + },[delay]) +} + function App() { const width = useWindowWidth(); + const [theme, setTheme] = useLocalStorage('theme','light'); + + const username = useInput( + '', + value => value.length < 3 ? 'минимум 3 символа' : null + ) + return ( <> - {/* Добавьте тестовый контент для проверки */}

Hello, React!{width}

+ +

+ { username.error ? username.error : '' } +

) }