Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de33066cbe |
Generated
+11
-27
@@ -8,7 +8,7 @@
|
|||||||
"name": "store-client",
|
"name": "store-client",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/react-query": "^5.100.14",
|
"immer": "^11.1.8",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -1305,32 +1305,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",
|
||||||
@@ -2081,6 +2055,16 @@
|
|||||||
"node": ">= 4"
|
"node": ">= 4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/immer": {
|
||||||
|
"version": "11.1.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
|
||||||
|
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/immer"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/import-fresh": {
|
"node_modules/import-fresh": {
|
||||||
"version": "3.3.1",
|
"version": "3.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/react-query": "^5.100.14",
|
"immer": "^11.1.8",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+41
-85
@@ -1,93 +1,49 @@
|
|||||||
import React, {useState, useEffect, useRef } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
//import { useQuery } from '@tanstack/react-query'
|
import { produce } from 'immer'
|
||||||
|
|
||||||
function useWindowWidth() {
|
|
||||||
const [width,setWidth] = useState(window.innerWidth)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const handleResize = () => setWidth(window.innerWidth);
|
|
||||||
window.addEventListener('resize',handleResize);
|
|
||||||
return () => window.removeEventListener('resize',handleResize);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
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() {
|
function App() {
|
||||||
const width = useWindowWidth();
|
const [tasks,setTasks] = useState([])
|
||||||
const [theme, setTheme] = useLocalStorage('theme','light');
|
const [input,setInput] = useState('')
|
||||||
|
|
||||||
const username = useInput(
|
|
||||||
'',
|
|
||||||
value => value.length < 3 ? 'минимум 3 символа' : null
|
|
||||||
)
|
|
||||||
|
|
||||||
|
const addTask = () => {
|
||||||
|
if(!input.trim()) return
|
||||||
|
|
||||||
|
setTasks(currentTasks => produce(currentTasks, draft => {
|
||||||
|
draft.push({
|
||||||
|
id: Date.now(),
|
||||||
|
text: input.trim(),
|
||||||
|
done: false
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
setInput('')
|
||||||
|
}
|
||||||
|
const toggleTask = id => {
|
||||||
|
setTasks(currentTask => produce(currentTask, draft => {
|
||||||
|
const task = draft.find(t => t.id === id)
|
||||||
|
if(task) {
|
||||||
|
task.done = !task.done
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
const removeTask = id => {
|
||||||
|
setTasks(currentTask => produce(currentTask, draft => {
|
||||||
|
const index = draft.findIndex(t => t.id === id)
|
||||||
|
if (index !== -1)
|
||||||
|
draft.splice(index,1)
|
||||||
|
}))
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!{width}</h1>
|
<h1>Практика по созданию списка задач</h1>
|
||||||
<button onClick={()=>{
|
<input value={input} onChange={e=>setInput(e.target.value)} />
|
||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
<button onClick={addTask}>Добавить</button>
|
||||||
}}>
|
<ul>{tasks.map(({id,text,done}) => (
|
||||||
Сменить тему
|
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
||||||
</button>
|
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
||||||
<p><input type="text"
|
{text}
|
||||||
value={username.value}
|
<button onClick={() => removeTask(id)}>Удалить</button>
|
||||||
onChange={username.onChange} />
|
</li>
|
||||||
{ username.error ? username.error : '' }
|
))}</ul>
|
||||||
</p>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export function MyComponent() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Hello</h1>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
export default MyComponent
|
||||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
<StrictMode>
|
|
||||||
<App />
|
<App />
|
||||||
</StrictMode>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user