Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aeb40b20 |
+31
-46
@@ -1,59 +1,44 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
||||||
|
import PostAddForm from './components/PostAddForm'
|
||||||
|
|
||||||
function memoize(fn) {
|
/*
|
||||||
const cache = new Map();
|
const mutation = useMutation({
|
||||||
return function(...args) {
|
mutationFn: newUser => {
|
||||||
const key = JSON.stringify(args);
|
fetch('https://jsonplaceholder.typicode.com/users', {
|
||||||
if (cache.has(key)) {
|
method: 'GET',
|
||||||
return cache.get(key);
|
headers: {'Content-type':'application/json'},
|
||||||
|
body: JSON.stringify(newUser),
|
||||||
|
}).then(res => res.json())
|
||||||
}
|
}
|
||||||
const result = fn.apply(this, args);
|
})
|
||||||
cache.set(key,result);
|
*/
|
||||||
return result;
|
|
||||||
|
const createQueryClient = () => {
|
||||||
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
staleTime: 5 * 60*1000,
|
||||||
|
retry: 1,
|
||||||
|
gcTime: 10*60*1000,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
retry:0,
|
||||||
|
onError: error => {console.error('Mutation error:',error.message)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
function fibonachi(n, memo={}) {
|
|
||||||
if (n in memo) {console.log(n);return memo[n];}
|
|
||||||
if (n <= 1) return 1;
|
|
||||||
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
|
||||||
return memo[n];
|
|
||||||
}
|
|
||||||
|
|
||||||
const MyComponent = ({ value }) => {
|
|
||||||
console.log('Rendring');
|
|
||||||
return <div>{value}</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
const MemoizedComponent = memo(MyComponent);
|
|
||||||
|
|
||||||
const Child = memo(({ children }) => {
|
|
||||||
console.log('Child component rendred');
|
|
||||||
return <div>{children}</div>;
|
|
||||||
});
|
|
||||||
|
|
||||||
const Parent = () => {
|
|
||||||
const [count,setCount] = useState(0);
|
|
||||||
const [text, setText] = useState('');
|
|
||||||
|
|
||||||
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Child>
|
|
||||||
{stableChild}
|
|
||||||
</Child>
|
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Parent />
|
<QueryClientProvider client={createQueryClient}>
|
||||||
|
<PostAddForm />
|
||||||
|
</QueryClientProvider>
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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