Compare commits
1 Commits
lesson-16
...
lesson-8.2
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
+51
-2
@@ -1,10 +1,59 @@
|
|||||||
// удаляем все, оставляя лишь пустую функцию App ниже
|
import { memo, useState, useMemo } from 'react'
|
||||||
|
|
||||||
|
function memoize(fn) {
|
||||||
|
const cache = new Map();
|
||||||
|
return function(...args) {
|
||||||
|
const key = JSON.stringify(args);
|
||||||
|
if (cache.has(key)) {
|
||||||
|
return cache.get(key);
|
||||||
|
}
|
||||||
|
const result = fn.apply(this, args);
|
||||||
|
cache.set(key,result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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