Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
Generated
-11
@@ -8,7 +8,6 @@
|
||||
"name": "store-client",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"immer": "^11.1.8",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
},
|
||||
@@ -2055,16 +2054,6 @@
|
||||
"node": ">= 4"
|
||||
}
|
||||
},
|
||||
"node_modules/immer": {
|
||||
"version": "11.1.8",
|
||||
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
|
||||
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/immer"
|
||||
}
|
||||
},
|
||||
"node_modules/import-fresh": {
|
||||
"version": "3.3.1",
|
||||
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"immer": "^11.1.8",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
},
|
||||
|
||||
+51
-41
@@ -1,49 +1,59 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import { produce } from 'immer'
|
||||
import { memo, useState, useMemo } from 'react'
|
||||
|
||||
function App() {
|
||||
const [tasks,setTasks] = useState([])
|
||||
const [input,setInput] = useState('')
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
const addTask = () => {
|
||||
if(!input.trim()) return
|
||||
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>,[]);
|
||||
|
||||
setTasks(currentTasks => produce(currentTasks, draft => {
|
||||
draft.push({
|
||||
id: Date.now(),
|
||||
text: input.trim(),
|
||||
done: false
|
||||
})
|
||||
}))
|
||||
setInput('')
|
||||
}
|
||||
const toggleTask = id => {
|
||||
setTasks(currentTask => produce(currentTask, draft => {
|
||||
const task = draft.find(t => t.id === id)
|
||||
if(task) {
|
||||
task.done = !task.done
|
||||
}
|
||||
}))
|
||||
}
|
||||
const removeTask = id => {
|
||||
setTasks(currentTask => produce(currentTask, draft => {
|
||||
const index = draft.findIndex(t => t.id === id)
|
||||
if (index !== -1)
|
||||
draft.splice(index,1)
|
||||
}))
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<h1>Практика по созданию списка задач</h1>
|
||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
||||
<button onClick={addTask}>Добавить</button>
|
||||
<ul>{tasks.map(({id,text,done}) => (
|
||||
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
||||
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
||||
{text}
|
||||
<button onClick={() => removeTask(id)}>Удалить</button>
|
||||
</li>
|
||||
))}</ul>
|
||||
<Child>
|
||||
{stableChild}
|
||||
</Child>
|
||||
<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 />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
export function MyComponent() {
|
||||
return (
|
||||
<>
|
||||
<h1>Hello</h1>
|
||||
</>
|
||||
)
|
||||
}
|
||||
export default MyComponent
|
||||
Reference in New Issue
Block a user