Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 7607146ff1 Разобрали useRef 2026-05-21 20:34:06 +03:00
3 changed files with 77 additions and 46 deletions
+76 -28
View File
@@ -1,50 +1,98 @@
import { useState, useReducer } from 'react' import { useState, useRef, useEffect } from 'react'
function counterReducer(state, action) { function Example() {
switch (action.type) { const myRef = useRef(null);
case 'increment': const counterRef = useRef(0);
return state + 1 const [text, setText] = useState('');
case 'decrement':
return state - 1 useEffect(() => {
case 'reset': myRef.current?.focus();
return 0 },[]);
case 'set_value':
return action.value const handlerFocus = () => {
default: if(myRef.current) {
return state 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 Counter() { function ClickCounter() {
const [count, dispatch] = useReducer(counterReducer,0); const countRef = useRef(0);
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> <div>
<span>Счетчик: {count}</span> Текущее значение: {value} <br />
<button onClick={()=> dispatch({type: 'increment'})}>+</button> Предыдущее значение: {prevValueRef.current}
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
</div> </div>
) )
} }
function App() { function ScrollList() {
const [tasks, setTasks] = useState([]) const listRef = useRef(null);
const [filter, setFilter] = useState('all')
const [sortBy,setSortBy] = useState('date')
const addTask = newTask => { const scrollToBottom = () => {
setTasks([...tasks,newTask]) 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() {
return ( return (
<> <>
{/* Добавьте тестовый контент для проверки */}
<h1>Hello, React!</h1> <h1>Hello, React!</h1>
<Counter /> <ScrollList />
</> </>
) )
} }
-15
View File
@@ -1,15 +0,0 @@
.container {
/* display: flex;
flex-direction: column;
flex-wrap: nowrap;*/
height: 500px;
overflow: auto;
border: 1px solid black;
}
.item {
font-size: 16pt;
border: 1px dotted gray;
/*margin: 5px 10px;
padding: 10px;*/
border-radius: 5px;
}
-2
View File
@@ -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>
) )