Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 35431f57d9 | |||
| 8a15a26ff6 |
Generated
+27
@@ -8,6 +8,7 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
@@ -1304,6 +1305,32 @@
|
|||||||
"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,6 +10,7 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
+74
-63
@@ -1,82 +1,93 @@
|
|||||||
import memo, { useMemo, useCallback, useState } from 'react'
|
import React, {useState, useEffect, useRef } from 'react'
|
||||||
|
//import { useQuery } from '@tanstack/react-query'
|
||||||
|
|
||||||
function MyComponent() {
|
function useWindowWidth() {
|
||||||
const [count, setCount] = useState(0);
|
const [width,setWidth] = useState(window.innerWidth)
|
||||||
const [name, setName] = useState('John');
|
|
||||||
|
|
||||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
useEffect(() => {
|
||||||
|
const handleResize = () => setWidth(window.innerWidth);
|
||||||
|
window.addEventListener('resize',handleResize);
|
||||||
|
return () => window.removeEventListener('resize',handleResize);
|
||||||
|
},[])
|
||||||
|
|
||||||
return (
|
return width
|
||||||
<>
|
|
||||||
<h2>{greeting}</h2>
|
|
||||||
<button onClick={()=>setName('Alex')}>New name</button>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function MyComponent2() {
|
function useLocalStorage(key, initValue) {
|
||||||
const [data,setData] = useState([{theme:'light'}])
|
const [value, setValue] = useState(()=>{
|
||||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
const stored = localStorage.getItem(key);
|
||||||
|
return stored !== null ? JSON.parse(stored) : initValue
|
||||||
|
})
|
||||||
|
|
||||||
const processData = useMemo(()=>{
|
useEffect(() => {
|
||||||
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
localStorage.setItem(key, JSON.stringify(value))
|
||||||
},[data, userPreferences]);
|
},[key, value])
|
||||||
console.log(processData);
|
|
||||||
return (
|
return [value,setValue]
|
||||||
<>
|
}
|
||||||
<h2 className={data.theme}>2</h2>
|
/*
|
||||||
<button onClick={()=>setData(data)}>set new theme</button>
|
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 MyComponent3() {
|
function useInterval(callback, delay) {
|
||||||
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
const savedCallback = useRef()
|
||||||
|
|
||||||
//Применение оправдано
|
useEffect(()=>{
|
||||||
const complexResult = useMemo(()=>{
|
savedCallback.current = callback
|
||||||
return heavyMethOperations(largeDataSet);
|
},[callback]);
|
||||||
},[largeDataSet])
|
|
||||||
}
|
|
||||||
|
|
||||||
function ParentComponent({ items }) {
|
useEffect(()=>{
|
||||||
const [filter, setFilter] = useState('');
|
if (delay == null) return
|
||||||
|
const id = setInterval(()=>{
|
||||||
const listProps = useMemo(()=>({
|
savedCallback.current()
|
||||||
items: items.filter(item => item.name.includes(filter)),
|
},delay);
|
||||||
onItemClick: id => console.log('Clicked',id),
|
return () => clearInterval(id);
|
||||||
theme: 'dark'
|
},[delay])
|
||||||
}),[items,filter]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Memoized {...listProps} />
|
|
||||||
<button onClick={()=>setFilter('Bob')}>Click</button>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Memoized(props) {
|
|
||||||
console.log(props);
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<h3 className={props.theme}>Memoized</h3>
|
|
||||||
{props.items.map((item) => (
|
|
||||||
<>
|
|
||||||
<li>
|
|
||||||
{item.name}
|
|
||||||
</li>
|
|
||||||
</>))}
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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>Hello, React!</h1>
|
<button onClick={()=>{
|
||||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
setTheme(theme === 'light' ? 'dark' : 'light')
|
||||||
|
}}>
|
||||||
|
Сменить тему
|
||||||
|
</button>
|
||||||
|
<p><input type="text"
|
||||||
|
value={username.value}
|
||||||
|
onChange={username.onChange} />
|
||||||
|
{ username.error ? username.error : '' }
|
||||||
|
</p>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,7 @@ 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