From ef33d16373ccbe4cef164e2fa1158f79e613a42d Mon Sep 17 00:00:00 2001 From: Laktionov Anton Date: Thu, 21 May 2026 21:33:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8=20Re?= =?UTF-8?q?act.memo=20=D0=B8=20React.useMemo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.jsx | 55 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.jsx | 2 -- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 6bf3ac6..90773fc 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,11 +1,60 @@ -// удаляем все, оставляя лишь пустую функцию 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
{value}
+} + +const MemoizedComponent = memo(MyComponent); + +const Child = memo(({ children }) => { + console.log('Child component rendred'); + return
{children}
; +}); + +const Parent = () => { + const [count,setCount] = useState(0); + const [text, setText] = useState(''); + + const stableChild = useMemo(() => Memo text,[]); + + return ( + <> + + {stableChild} + + setText(e.target.value)} value={text} /> + + + ) +} function App() { return ( <> - {/* Добавьте тестовый контент для проверки */}

Hello, React!

- + + ) } diff --git a/src/main.jsx b/src/main.jsx index 950e96a..7e925e8 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client' import App from './App.jsx' createRoot(document.getElementById('root')).render( - - )