Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+73
-53
@@ -1,78 +1,98 @@
|
||||
import { useState, createContext, useContext } from 'react'
|
||||
import styles from './App.module.scss'
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
|
||||
const UserContext = createContext('')
|
||||
|
||||
function UserProvider({ children }) {
|
||||
const [name,setName] = useState('');
|
||||
function Example() {
|
||||
const myRef = useRef(null);
|
||||
const counterRef = useRef(0);
|
||||
const [text, setText] = useState('');
|
||||
|
||||
const toggleName = (newName) => {
|
||||
setName(newName);
|
||||
}
|
||||
useEffect(() => {
|
||||
myRef.current?.focus();
|
||||
},[]);
|
||||
|
||||
const handlerFocus = () => {
|
||||
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) => {
|
||||
setNewName(event.target.value);
|
||||
setText(event.target.value);
|
||||
}
|
||||
return (
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input type='text' value={newName} onChange={handlerChange} />
|
||||
<button type='submit'>Войти</button>
|
||||
</form>
|
||||
)
|
||||
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 ExitButton() {
|
||||
const {name, toggleName} = useContext(UserContext)
|
||||
function ClickCounter() {
|
||||
const countRef = useRef(0);
|
||||
|
||||
return (
|
||||
<button onClick={() => toggleName('')}>Выход</button>
|
||||
)
|
||||
function handlerClick() {
|
||||
countRef.current += 1;
|
||||
//alert(`Нажали ${countRef.current} раз.`);
|
||||
}
|
||||
return <button onClick={handlerClick}>Нажать</button>;
|
||||
}
|
||||
|
||||
function Toolbar() {
|
||||
const {name, toggleName} = useContext(UserContext)
|
||||
return(
|
||||
<div className={styles.container}>
|
||||
<h3>Панель пользователя</h3>
|
||||
{name && `Привет ${name}`}
|
||||
{name ? <ExitButton /> : <Form /> }
|
||||
|
||||
</div>
|
||||
)
|
||||
function TimerComponent() {
|
||||
const timerIdRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
timerIdRef.current = setTimeout(() => {
|
||||
alert("Прошло 3 секунды");
|
||||
}, 3000);
|
||||
return () => {
|
||||
clearTimeout(timerIdRef.current);
|
||||
}
|
||||
},[]);
|
||||
}
|
||||
|
||||
function Page() {
|
||||
function PrevValue({ value }) {
|
||||
const prevValueRef = useRef();
|
||||
|
||||
useEffect(() => {
|
||||
prevValueRef.current = value;
|
||||
});
|
||||
return (
|
||||
<div>
|
||||
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||
<Toolbar />
|
||||
Текущее значение: {value} <br />
|
||||
Предыдущее значение: {prevValueRef.current}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
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() {
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<h1>Hello, React!</h1>
|
||||
<UserProvider>
|
||||
<Page />
|
||||
</UserProvider>
|
||||
<ScrollList />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user