Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6827c800f1 | |||
| 5ee1bf2eee | |||
| c137c644e3 |
+45
-85
@@ -1,98 +1,58 @@
|
|||||||
import { useState, useRef, useEffect } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function Example() {
|
class RandomUser extends React.Component {
|
||||||
const myRef = useRef(null);
|
constructor(props) {
|
||||||
const counterRef = useRef(0);
|
super(props)
|
||||||
const [text, setText] = useState('');
|
this.state = { user: {} }
|
||||||
|
|
||||||
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 (
|
componentDidMount() {
|
||||||
<>
|
fetch('https://randomuser.me/api')
|
||||||
<div ref={listRef}
|
.then(responce => responce.json())
|
||||||
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
{[...Array(20)].map((_,i)=>(
|
console.log('смонтирован')
|
||||||
<div key={i}>Элемент № {i + 1}</div>
|
}
|
||||||
))}
|
|
||||||
|
componentDidUpdate(prevProps,prevState) {
|
||||||
|
console.log(this.state);
|
||||||
|
console.log('Компонент обнвлен')
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
console.log('Компонент таймера размонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <div>
|
||||||
|
{this.state.user &&
|
||||||
|
this.state.user.name &&
|
||||||
|
(
|
||||||
|
<>
|
||||||
|
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
||||||
|
<img src={this.state.user.picture.medium} />
|
||||||
|
<button onClick={() => {
|
||||||
|
console.log('Загружаем ... ')
|
||||||
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
}}>Загрузить нового</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
}
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<ScrollList />
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
|
</button>
|
||||||
|
{showClock && <RandomUser />}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user