Compare commits
1 Commits
lesson-17
...
lesson-8.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+90
-2
@@ -1,10 +1,98 @@
|
|||||||
// удаляем все, оставляя лишь пустую функцию App ниже
|
import { useState, useRef, useEffect } from 'react'
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<div>
|
||||||
|
Текущее значение: {value} <br />
|
||||||
|
Предыдущее значение: {prevValueRef.current}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
|
||||||
<h1>Hello, React!</h1>
|
<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