Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as ef33d16373 Сделали React.memo и React.useMemo 2026-05-21 21:33:02 +03:00
4 changed files with 60 additions and 1282 deletions
+9 -1256
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -10,7 +10,6 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"info": "^1.0.0",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0"
}, },
@@ -23,8 +22,6 @@
"eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19", "eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0", "globals": "^16.0.0",
"sass": "^1.99.0",
"sass-embedded": "^1.99.0",
"vite": "^6.3.5" "vite": "^6.3.5"
} }
} }
+49 -19
View File
@@ -1,29 +1,59 @@
import styles from './styles/App.module.scss' import { memo, useState, useMemo } from 'react'
import React, { useState, useCallback } from 'react'
function CounterButton({label, onClick}) { function memoize(fn) {
return ( const cache = new Map();
<button onClick={onClick} className={styles.counter_button}>{label}</button> return function(...args) {
) const key = JSON.stringify(args);
} if (cache.has(key)) {
function CounterDisplay({value}) { return cache.get(key);
return (<div className={styles.counter_display}> }
Значение счетчика: <b>{value}</b> const result = fn.apply(this, args);
</div>) cache.set(key,result);
return result;
}
} }
function App() { function fibonachi(n, memo={}) {
const [counter,setCounter] = useState(0); 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 increment = () => setCounter(prev => prev + 1); const MyComponent = ({ value }) => {
const decrement = () => setCounter(prev => prev - 1); 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 ( return (
<> <>
<h2>Практика по созданию счетчика с колбэками</h2> <Child>
<CounterButton label="Увеличить" onClick={increment} /> {stableChild}
<CounterButton label="уменьшить" onClick={decrement} /> </Child>
<CounterDisplay value={counter} /> <input type="text" onChange={(e) => setText(e.target.value)} value={text} />
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
</>
)
}
function App() {
return (
<>
<h1>Hello, React!</h1>
<Parent />
</> </>
) )
} }
-2
View File
@@ -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>
) )