Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 7607146ff1 Разобрали useRef 2026-05-21 20:34:06 +03:00
5 changed files with 97 additions and 1302 deletions
+9 -1256
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -10,7 +10,6 @@
"preview": "vite preview"
},
"dependencies": {
"info": "^1.0.0",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
@@ -23,8 +22,6 @@
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"sass": "^1.99.0",
"sass-embedded": "^1.99.0",
"vite": "^6.3.5"
}
}
+87 -18
View File
@@ -1,29 +1,98 @@
import styles from './styles/App.module.scss'
import React, { useState, useCallback } from 'react'
import { useState, useRef, useEffect } from 'react'
function CounterButton({label, onClick}) {
function Example() {
const myRef = useRef(null);
const counterRef = useRef(0);
const [text, setText] = useState('');
useEffect(() => {
myRef.current?.focus();
},[]);
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>
}
function ClickCounter() {
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 (
<button onClick={onClick} className={styles.counter_button}>{label}</button>
<div>
Текущее значение: {value} <br />
Предыдущее значение: {prevValueRef.current}
</div>
)
}
function CounterDisplay({value}) {
return (<div className={styles.counter_display}>
Значение счетчика: <b>{value}</b>
</div>)
function ScrollList() {
const listRef = useRef(null);
const scrollToBottom = () => {
if(listRef.current) {
listRef.current.scrollTop = listRef.current.scrollHeight;
}
}
function App() {
const [counter,setCounter] = useState(0);
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
return (
<>
<h2>Практика по созданию счетчика с колбэками</h2>
<CounterButton label="Увеличить" onClick={increment} />
<CounterButton label="уменьшить" onClick={decrement} />
<CounterDisplay value={counter} />
<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 (
<>
<h1>Hello, React!</h1>
<ScrollList />
</>
)
}
+1 -3
View File
@@ -1,10 +1,8 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
// удаляем import './index.css'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
)
-22
View File
@@ -1,22 +0,0 @@
.container {
display:flex;
flex-direction: row;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
p {
padding: 5px 10px;
}
}
.counter {
&_display{
border: 1px solid black;
border-width: 3px;
border-radius: 10px;
padding: 10px;
width: 300px;
}
&_button{
padding: 10px;
}
}