Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
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"
|
||||||
},
|
},
|
||||||
|
|||||||
+57
-72
@@ -1,93 +1,78 @@
|
|||||||
import React, {useState, useEffect, useRef } from 'react'
|
import { useState, createContext, useContext } from 'react'
|
||||||
//import { useQuery } from '@tanstack/react-query'
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
function useWindowWidth() {
|
const UserContext = createContext('')
|
||||||
const [width,setWidth] = useState(window.innerWidth)
|
|
||||||
|
|
||||||
useEffect(() => {
|
function UserProvider({ children }) {
|
||||||
const handleResize = () => setWidth(window.innerWidth);
|
const [name,setName] = useState('');
|
||||||
window.addEventListener('resize',handleResize);
|
|
||||||
return () => window.removeEventListener('resize',handleResize);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return width
|
const toggleName = (newName) => {
|
||||||
|
setName(newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
function useLocalStorage(key, initValue) {
|
return (
|
||||||
const [value, setValue] = useState(()=>{
|
<UserContext.Provider value={{name, toggleName}}>
|
||||||
const stored = localStorage.getItem(key);
|
{children}
|
||||||
return stored !== null ? JSON.parse(stored) : initValue
|
</UserContext.Provider>
|
||||||
})
|
|
||||||
|
|
||||||
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 Form() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
const [newName,setNewName] = useState('');
|
||||||
|
const handleSubmit = (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
toggleName(event.target[0].value)
|
||||||
|
}
|
||||||
|
const handlerChange = (event) => {
|
||||||
|
setNewName(event.target.value);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type='text' value={newName} onChange={handlerChange} />
|
||||||
|
<button type='submit'>Войти</button>
|
||||||
|
</form>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function useInterval(callback, delay) {
|
function ExitButton() {
|
||||||
const savedCallback = useRef()
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
|
||||||
useEffect(()=>{
|
return (
|
||||||
savedCallback.current = callback
|
<button onClick={() => toggleName('')}>Выход</button>
|
||||||
},[callback]);
|
)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(()=>{
|
function Toolbar() {
|
||||||
if (delay == null) return
|
const {name, toggleName} = useContext(UserContext)
|
||||||
const id = setInterval(()=>{
|
return(
|
||||||
savedCallback.current()
|
<div className={styles.container}>
|
||||||
},delay);
|
<h3>Панель пользователя</h3>
|
||||||
return () => clearInterval(id);
|
{name && `Привет ${name}`}
|
||||||
},[delay])
|
{name ? <ExitButton /> : <Form /> }
|
||||||
|
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Page() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||||
|
<Toolbar />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
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>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<button onClick={()=>{
|
<h1>Hello, React!</h1>
|
||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
<UserProvider>
|
||||||
}}>
|
<Page />
|
||||||
Сменить тему
|
</UserProvider>
|
||||||
</button>
|
|
||||||
<p><input type="text"
|
|
||||||
value={username.value}
|
|
||||||
onChange={username.onChange} />
|
|
||||||
{ username.error ? username.error : '' }
|
|
||||||
</p>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
.container {
|
||||||
|
height: 500px;
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 10px;
|
||||||
|
background-color: lightblue;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user