Compare commits

..

3 Commits

3 changed files with 74 additions and 79 deletions
+59 -79
View File
@@ -1,98 +1,78 @@
import { useState, useRef, useEffect } from 'react' import { useState, createContext, useContext } from 'react'
import styles from './App.module.scss'
function Example() { const UserContext = createContext('')
const myRef = useRef(null);
const counterRef = useRef(0); function UserProvider({ children }) {
const [text, setText] = useState(''); const [name,setName] = useState('');
useEffect(() => { const toggleName = (newName) => {
myRef.current?.focus(); setName(newName);
},[]);
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 ( return (
<div> <UserContext.Provider value={{name, toggleName}}>
Текущее значение: {value} <br /> {children}
Предыдущее значение: {prevValueRef.current} </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) => {
setNewName(event.target.value);
}
return (
<form onSubmit={handleSubmit}>
<input type='text' value={newName} onChange={handlerChange} />
<button type='submit'>Войти</button>
</form>
)
}
function ExitButton() {
const {name, toggleName} = useContext(UserContext)
return (
<button onClick={() => toggleName('')}>Выход</button>
)
}
function Toolbar() {
const {name, toggleName} = useContext(UserContext)
return(
<div className={styles.container}>
<h3>Панель пользователя</h3>
{name && `Привет ${name}`}
{name ? <ExitButton /> : <Form /> }
</div> </div>
) )
} }
function ScrollList() { function Page() {
const listRef = useRef(null);
const scrollToBottom = () => {
if(listRef.current) {
listRef.current.scrollTop = listRef.current.scrollHeight;
}
}
return ( return (
<> <div>
<div ref={listRef} <h1 style={{textAlign: 'center'}}>Главная страница</h1>
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}> <Toolbar />
{[...Array(20)].map((_,i)=>( </div>
<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 /> <UserProvider>
<Page />
</UserProvider>
</> </>
) )
} }
+13
View File
@@ -0,0 +1,13 @@
.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,5 +4,7 @@ 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>
) )