Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 0e043df71e Конец урока 2026-06-11 19:14:13 +03:00
2 changed files with 55 additions and 85 deletions
+53 -85
View File
@@ -1,99 +1,67 @@
import { useState, useRef, useEffect } from 'react' import React, { useState, useReducer, useRef, memo } from 'react'
function Example() { function tasksReducer(state, action) {
const myRef = useRef(null); switch(action.type) {
const counterRef = useRef(0); case 'ADD_TASK':
const [text, setText] = useState(''); return [...state, action.payload]
case 'TOGGLE_TASK':
useEffect(() => { return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
myRef.current?.focus(); case 'REMOVE_TASK':
},[]); return state.filter(task => task.id !== action.payload)
default:
const handlerFocus = () => { return state
if(myRef.current) {
myRef.current.focus();
}
};
const handlerChange = (event) => {
setText(event.target.value);
} }
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>
} }
function ClickCounter() { const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
const countRef = useRef(0); console.log("Rendred")
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 ( return (
<div> <ul>
Текущее значение: {value} <br /> {tasks.map(({id, text, complite}) => (
Предыдущее значение: {prevValueRef.current} <li key={id}
</div> style={{textDecoration: complite ? 'line-through' : 'none' }}>
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
{text}
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
Удалить
</button>
</li>
))}
</ul>
) )
} })
function ScrollList() {
const listRef = useRef(null);
const scrollToBottom = () => {
if(listRef.current) {
listRef.current.scrollTop = listRef.current.scrollHeight;
}
}
return (
<>
<div ref={listRef}
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
{[...Array(20)].map((_,i)=>(
<div key={i}>Элемент {i + 1}</div>
))}
</div>
<button onClick={scrollToBottom}>Прокрутить вниз</button>
</>
)
}
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>
<ScrollList /> <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>
) )
} }
+2
View File
@@ -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>
) )