Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
Generated
-11
@@ -8,7 +8,6 @@
|
|||||||
"name": "store-client",
|
"name": "store-client",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -2055,16 +2054,6 @@
|
|||||||
"node": ">= 4"
|
"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": {
|
"node_modules/import-fresh": {
|
||||||
"version": "3.3.1",
|
"version": "3.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+86
-37
@@ -1,49 +1,98 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import { useState, useRef, useEffect } from 'react'
|
||||||
import { produce } from 'immer'
|
|
||||||
|
|
||||||
function App() {
|
function Example() {
|
||||||
const [tasks,setTasks] = useState([])
|
const myRef = useRef(null);
|
||||||
const [input,setInput] = useState('')
|
const counterRef = useRef(0);
|
||||||
|
const [text, setText] = useState('');
|
||||||
|
|
||||||
const addTask = () => {
|
useEffect(() => {
|
||||||
if(!input.trim()) return
|
myRef.current?.focus();
|
||||||
|
},[]);
|
||||||
|
|
||||||
setTasks(currentTasks => produce(currentTasks, draft => {
|
const handlerFocus = () => {
|
||||||
draft.push({
|
if(myRef.current) {
|
||||||
id: Date.now(),
|
myRef.current.focus();
|
||||||
text: input.trim(),
|
|
||||||
done: false
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
setInput('')
|
|
||||||
}
|
}
|
||||||
const toggleTask = id => {
|
};
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
|
||||||
const task = draft.find(t => t.id === id)
|
const handlerChange = (event) => {
|
||||||
if(task) {
|
setText(event.target.value);
|
||||||
task.done = !task.done
|
|
||||||
}
|
}
|
||||||
}))
|
counterRef.current += 1;
|
||||||
|
return <div>
|
||||||
|
<input ref={myRef} text="text" /><br />
|
||||||
|
Количество рендров: {counterRef.current}<br />
|
||||||
|
<input text="text" onChange={handlerChange} value={text} />
|
||||||
|
<button onClick={handlerFocus}>Поставить фокус на поле</button>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
const removeTask = id => {
|
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
function ClickCounter() {
|
||||||
const index = draft.findIndex(t => t.id === id)
|
const countRef = useRef(0);
|
||||||
if (index !== -1)
|
|
||||||
draft.splice(index,1)
|
function handlerClick() {
|
||||||
}))
|
countRef.current += 1;
|
||||||
|
//alert(`Нажали ${countRef.current} раз.`);
|
||||||
}
|
}
|
||||||
|
return <button onClick={handlerClick}>Нажать</button>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function TimerComponent() {
|
||||||
|
const timerIdRef = useRef(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
timerIdRef.current = setTimeout(() => {
|
||||||
|
alert("Прошло 3 секунды");
|
||||||
|
}, 3000);
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timerIdRef.current);
|
||||||
|
}
|
||||||
|
},[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PrevValue({ value }) {
|
||||||
|
const prevValueRef = useRef();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
prevValueRef.current = value;
|
||||||
|
});
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
Текущее значение: {value} <br />
|
||||||
|
Предыдущее значение: {prevValueRef.current}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ScrollList() {
|
||||||
|
const listRef = useRef(null);
|
||||||
|
|
||||||
|
const scrollToBottom = () => {
|
||||||
|
if(listRef.current) {
|
||||||
|
listRef.current.scrollTop = listRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Практика по созданию списка задач</h1>
|
<div ref={listRef}
|
||||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
||||||
<button onClick={addTask}>Добавить</button>
|
{[...Array(20)].map((_,i)=>(
|
||||||
<ul>{tasks.map(({id,text,done}) => (
|
<div key={i}>Элемент № {i + 1}</div>
|
||||||
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
))}
|
||||||
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
</div>
|
||||||
{text}
|
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||||
<button onClick={() => removeTask(id)}>Удалить</button>
|
</>
|
||||||
</li>
|
)
|
||||||
))}</ul>
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Hello, React!</h1>
|
||||||
|
<ScrollList />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
export function MyComponent() {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<h1>Hello</h1>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export default MyComponent
|
|
||||||
Reference in New Issue
Block a user