Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23db798577 | |||
| c99a045400 |
+34
-43
@@ -1,59 +1,50 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import { useState, useReducer } from 'react'
|
||||||
|
|
||||||
function memoize(fn) {
|
function counterReducer(state, action) {
|
||||||
const cache = new Map();
|
switch (action.type) {
|
||||||
return function(...args) {
|
case 'increment':
|
||||||
const key = JSON.stringify(args);
|
return state + 1
|
||||||
if (cache.has(key)) {
|
case 'decrement':
|
||||||
return cache.get(key);
|
return state - 1
|
||||||
}
|
case 'reset':
|
||||||
const result = fn.apply(this, args);
|
return 0
|
||||||
cache.set(key,result);
|
case 'set_value':
|
||||||
return result;
|
return action.value
|
||||||
|
default:
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fibonachi(n, memo={}) {
|
function Counter() {
|
||||||
if (n in memo) {console.log(n);return memo[n];}
|
const [count, dispatch] = useReducer(counterReducer,0);
|
||||||
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 (
|
return (
|
||||||
<>
|
<div>
|
||||||
<Child>
|
<span>Счетчик: {count}</span>
|
||||||
{stableChild}
|
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||||
</Child>
|
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||||
</>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [tasks, setTasks] = useState([])
|
||||||
|
const [filter, setFilter] = useState('all')
|
||||||
|
const [sortBy,setSortBy] = useState('date')
|
||||||
|
|
||||||
|
const addTask = newTask => {
|
||||||
|
setTasks([...tasks,newTask])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Parent />
|
<Counter />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
.container {
|
||||||
|
/* display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
flex-wrap: nowrap;*/
|
||||||
|
height: 500px;
|
||||||
|
overflow: auto;
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
@@ -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