Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+55
-48
@@ -1,60 +1,67 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
|
|
||||||
function memoize(fn) {
|
function tasksReducer(state, action) {
|
||||||
const cache = new Map();
|
switch(action.type) {
|
||||||
return function(...args) {
|
case 'ADD_TASK':
|
||||||
const key = JSON.stringify(args);
|
return [...state, action.payload]
|
||||||
if (cache.has(key)) {
|
case 'TOGGLE_TASK':
|
||||||
return cache.get(key);
|
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||||
}
|
case 'REMOVE_TASK':
|
||||||
const result = fn.apply(this, args);
|
return state.filter(task => task.id !== action.payload)
|
||||||
cache.set(key,result);
|
default:
|
||||||
return result;
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fibonachi(n, memo={}) {
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
if (n in memo) {console.log(n);return memo[n];}
|
console.log("Rendred")
|
||||||
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 (
|
||||||
<>
|
<ul>
|
||||||
<Child>
|
{tasks.map(({id, text, complite}) => (
|
||||||
{stableChild}
|
<li key={id}
|
||||||
</Child>
|
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
{text}
|
||||||
</>
|
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||||
|
Удалить
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
||||||
|
const [input,setInput] = useState('')
|
||||||
|
const [error,setError] = useState('')
|
||||||
|
const inputRef = useRef(null)
|
||||||
|
|
||||||
|
const addTask = () => {
|
||||||
|
if (input.trim() === '') {
|
||||||
|
setError('Пустой текст задачи')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setError('')
|
||||||
|
dispatch({
|
||||||
|
type: 'ADD_TASK',
|
||||||
|
payload: {id: Date.now(), text: input.trim(), complite: false }
|
||||||
|
})
|
||||||
|
setInput('')
|
||||||
|
inputRef.current.focus()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div style={{padding: 20}}>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Список дел</h1>
|
||||||
<Parent />
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
</>
|
<button onClick={addTask}>Добавить</button>
|
||||||
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
|
|
||||||
|
<TaskList tasks={tasks}
|
||||||
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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