Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
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",
|
||||||
|
|||||||
+1
-2
@@ -4,13 +4,12 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
+55
-82
@@ -1,94 +1,67 @@
|
|||||||
import React, {useState, useEffect, useRef } from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
//import { useQuery } from '@tanstack/react-query'
|
|
||||||
|
|
||||||
function useWindowWidth() {
|
function tasksReducer(state, action) {
|
||||||
const [width,setWidth] = useState(window.innerWidth)
|
switch(action.type) {
|
||||||
|
case 'ADD_TASK':
|
||||||
useEffect(() => {
|
return [...state, action.payload]
|
||||||
const handleResize = () => setWidth(window.innerWidth);
|
case 'TOGGLE_TASK':
|
||||||
window.addEventListener('resize',handleResize);
|
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||||
return () => window.removeEventListener('resize',handleResize);
|
case 'REMOVE_TASK':
|
||||||
},[])
|
return state.filter(task => task.id !== action.payload)
|
||||||
|
default:
|
||||||
return width
|
return state
|
||||||
}
|
|
||||||
|
|
||||||
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 TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
const savedCallback = useRef()
|
console.log("Rendred")
|
||||||
|
return (
|
||||||
useEffect(()=>{
|
<ul>
|
||||||
savedCallback.current = callback
|
{tasks.map(({id, text, complite}) => (
|
||||||
},[callback]);
|
<li key={id}
|
||||||
|
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||||
useEffect(()=>{
|
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||||
if (delay == null) return
|
{text}
|
||||||
const id = setInterval(()=>{
|
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||||
savedCallback.current()
|
Удалить
|
||||||
},delay);
|
</button>
|
||||||
return () => clearInterval(id);
|
</li>
|
||||||
},[delay])
|
))}
|
||||||
}
|
</ul>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const width = useWindowWidth();
|
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
||||||
const [theme, setTheme] = useLocalStorage('theme','light');
|
const [input,setInput] = useState('')
|
||||||
|
const [error,setError] = useState('')
|
||||||
|
const inputRef = useRef(null)
|
||||||
|
|
||||||
const username = useInput(
|
const addTask = () => {
|
||||||
'',
|
if (input.trim() === '') {
|
||||||
value => value.length < 3 ? 'минимум 3 символа' : null
|
setError('Пустой текст задачи')
|
||||||
)
|
return
|
||||||
|
}
|
||||||
|
setError('')
|
||||||
|
dispatch({
|
||||||
|
type: 'ADD_TASK',
|
||||||
|
payload: {id: Date.now(), text: input.trim(), complite: false }
|
||||||
|
})
|
||||||
|
setInput('')
|
||||||
|
inputRef.current.focus()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div style={{padding: 20}}>
|
||||||
<h1>Hello, React!{width}</h1>
|
<h1>Список дел</h1>
|
||||||
<button onClick={()=>{
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
<button onClick={addTask}>Добавить</button>
|
||||||
}}>
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
Сменить тему
|
|
||||||
</button>
|
<TaskList tasks={tasks}
|
||||||
<p><input type="text"
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
value={username.value}
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
onChange={username.onChange} />
|
</div>
|
||||||
{ username.error ? username.error : '' }
|
|
||||||
</p>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user