Compare commits

..

2 Commits

3 changed files with 52 additions and 44 deletions
+34 -43
View File
@@ -1,59 +1,50 @@
import { memo, useState, useMemo } from 'react'
import { useState, useReducer } 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 counterReducer(state, action) {
switch (action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
case 'reset':
return 0
case 'set_value':
return action.value
default:
return state
}
}
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>,[]);
function Counter() {
const [count, dispatch] = useReducer(counterReducer,0);
return (
<>
<Child>
{stableChild}
</Child>
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
</>
<div>
<span>Счетчик: {count}</span>
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
</div>
)
}
function App() {
const [tasks, setTasks] = useState([])
const [filter, setFilter] = useState('all')
const [sortBy,setSortBy] = useState('date')
const addTask = newTask => {
setTasks([...tasks,newTask])
}
return (
<>
{/* Добавьте тестовый контент для проверки */}
<h1>Hello, React!</h1>
<Parent />
<Counter />
</>
)
}
+15
View File
@@ -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;
}
+2
View File
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
)