Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+86
-54
@@ -1,67 +1,99 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import { useState, useRef, useEffect } from 'react'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
function Example() {
|
||||||
switch(action.type) {
|
const myRef = useRef(null);
|
||||||
case 'ADD_TASK':
|
const counterRef = useRef(0);
|
||||||
return [...state, action.payload]
|
const [text, setText] = useState('');
|
||||||
case 'TOGGLE_TASK':
|
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
useEffect(() => {
|
||||||
case 'REMOVE_TASK':
|
myRef.current?.focus();
|
||||||
return state.filter(task => task.id !== action.payload)
|
},[]);
|
||||||
default:
|
|
||||||
return state
|
const handlerFocus = () => {
|
||||||
|
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>
|
||||||
}
|
}
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
function ClickCounter() {
|
||||||
console.log("Rendred")
|
const countRef = useRef(0);
|
||||||
return (
|
|
||||||
<ul>
|
|
||||||
{tasks.map(({id, text, complite}) => (
|
|
||||||
<li key={id}
|
|
||||||
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 App() {
|
function handlerClick() {
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
countRef.current += 1;
|
||||||
const [input,setInput] = useState('')
|
//alert(`Нажали ${countRef.current} раз.`);
|
||||||
const [error,setError] = useState('')
|
}
|
||||||
const inputRef = useRef(null)
|
return <button onClick={handlerClick}>Нажать</button>;
|
||||||
|
}
|
||||||
|
|
||||||
const addTask = () => {
|
function TimerComponent() {
|
||||||
if (input.trim() === '') {
|
const timerIdRef = useRef(null);
|
||||||
setError('Пустой текст задачи')
|
|
||||||
return
|
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;
|
||||||
}
|
}
|
||||||
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>Список дел</h1>
|
<div ref={listRef}
|
||||||
<input ref={inputRef} 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)=>(
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
<div key={i}>Элемент № {i + 1}</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
<TaskList tasks={tasks}
|
function App() {
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
|
||||||
</div>
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Hello, React!</h1>
|
||||||
|
<ScrollList />
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,5 @@ 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