Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
Generated
+9
-1256
File diff suppressed because it is too large
Load Diff
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+50
-20
@@ -1,30 +1,60 @@
|
|||||||
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)) {
|
||||||
|
return cache.get(key);
|
||||||
|
}
|
||||||
|
const result = fn.apply(this, args);
|
||||||
|
cache.set(key,result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function CounterDisplay({value}) {
|
|
||||||
return (<div className={styles.counter_display}>
|
function fibonachi(n, memo={}) {
|
||||||
Значение счетчика: <b>{value}</b>
|
if (n in memo) {console.log(n);return memo[n];}
|
||||||
</div>)
|
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() {
|
||||||
const [counter,setCounter] = useState(0);
|
|
||||||
|
|
||||||
const increment = () => setCounter(prev => prev + 1);
|
|
||||||
const decrement = () => setCounter(prev => prev - 1);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h2>Практика по созданию счетчика с колбэками</h2>
|
<h1>Hello, React!</h1>
|
||||||
<CounterButton label="Увеличить" onClick={increment} />
|
<Parent />
|
||||||
<CounterButton label="уменьшить" onClick={decrement} />
|
</>
|
||||||
<CounterDisplay value={counter} />
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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