Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 7607146ff1 Разобрали useRef 2026-05-21 20:34:06 +03:00
3 changed files with 73 additions and 68 deletions
+70 -50
View File
@@ -1,78 +1,98 @@
import { useState, createContext, useContext } from 'react' import { useState, useRef, useEffect } from 'react'
import styles from './App.module.scss'
const UserContext = createContext('') function Example() {
const myRef = useRef(null);
const counterRef = useRef(0);
const [text, setText] = useState('');
function UserProvider({ children }) { useEffect(() => {
const [name,setName] = useState(''); myRef.current?.focus();
},[]);
const toggleName = (newName) => { const handlerFocus = () => {
setName(newName); if(myRef.current) {
} myRef.current.focus();
}
};
return (
<UserContext.Provider value={{name, toggleName}}>
{children}
</UserContext.Provider>
)
}
function Form() {
const {name, toggleName} = useContext(UserContext)
const [newName,setNewName] = useState('');
const handleSubmit = (event) => {
event.preventDefault();
toggleName(event.target[0].value)
}
const handlerChange = (event) => { const handlerChange = (event) => {
setNewName(event.target.value); setText(event.target.value);
} }
return ( counterRef.current += 1;
<form onSubmit={handleSubmit}> return <div>
<input type='text' value={newName} onChange={handlerChange} /> <input ref={myRef} text="text" /><br />
<button type='submit'>Войти</button> Количество рендров: {counterRef.current}<br />
</form> <input text="text" onChange={handlerChange} value={text} />
) <button onClick={handlerFocus}>Поставить фокус на поле</button>
</div>
} }
function ExitButton() { function ClickCounter() {
const {name, toggleName} = useContext(UserContext) const countRef = useRef(0);
return ( function handlerClick() {
<button onClick={() => toggleName('')}>Выход</button> countRef.current += 1;
) //alert(`Нажали ${countRef.current} раз.`);
}
return <button onClick={handlerClick}>Нажать</button>;
} }
function Toolbar() { function TimerComponent() {
const {name, toggleName} = useContext(UserContext) const timerIdRef = useRef(null);
return(
<div className={styles.container}>
<h3>Панель пользователя</h3>
{name && `Привет ${name}`}
{name ? <ExitButton /> : <Form /> }
</div> useEffect(() => {
) timerIdRef.current = setTimeout(() => {
alert("Прошло 3 секунды");
}, 3000);
return () => {
clearTimeout(timerIdRef.current);
}
},[]);
} }
function Page() { function PrevValue({ value }) {
const prevValueRef = useRef();
useEffect(() => {
prevValueRef.current = value;
});
return ( return (
<div> <div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1> Текущее значение: {value} <br />
<Toolbar /> Предыдущее значение: {prevValueRef.current}
</div> </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>
<UserProvider> <ScrollList />
<Page />
</UserProvider>
</> </>
) )
} }
-13
View File
@@ -1,13 +0,0 @@
.container {
height: 500px;
border: 1px solid black;
border-radius: 10px;
background-color: lightblue;
}
.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>
) )