Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12515f925d | |||
| b3662b759f |
+97
-87
@@ -1,98 +1,108 @@
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
import React 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);
|
||||
class MyComponent extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { data: null };
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
}
|
||||
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>
|
||||
))}
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
{this.state.data ? (
|
||||
<div>Данные: {this.state.data}</div>
|
||||
) : (
|
||||
<div>Загрузка...</div>
|
||||
)}
|
||||
<button onClick={this.handleClick}>Обновить</button>
|
||||
</div>
|
||||
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
)
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://api.example.com/data')
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({data: json}));
|
||||
|
||||
this.timerId = setInterval(()=> console.log('Tick'), 1000);
|
||||
}
|
||||
|
||||
componentWillUnmout() {
|
||||
clearInterval(this.timerId);
|
||||
}
|
||||
|
||||
handleClick() {
|
||||
this.setState({data: 'Данные'})
|
||||
}
|
||||
}
|
||||
|
||||
class MyComponent2 extends React.Component {
|
||||
controller = new AbortController()
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({data: json}))
|
||||
.catch(error => {
|
||||
if(error.name == 'AbortError') {
|
||||
console.log('Aborted');
|
||||
} else {
|
||||
console.log('Other error',error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.controller.abort()
|
||||
}
|
||||
}
|
||||
class MyComponent3 extends React.Component {
|
||||
componentDidMount() {
|
||||
this.loadData();
|
||||
}
|
||||
|
||||
async loadData() {
|
||||
try {
|
||||
const response = await fetch('https://api.example.com/data');
|
||||
const data = await response.json();
|
||||
this.setSate({ data });
|
||||
} catch {
|
||||
console.log('error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Timer extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { seconds: 0 }
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
console.log('Timer mounted');
|
||||
this.interval = setInterval(()=>{
|
||||
this.setState(prevState => ({seconds: prevState.seconds + 1}))
|
||||
},1000);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
console.log('Timer unmount');
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
render() {
|
||||
return <h2>Прошло секунд: {this.state.seconds}</h2>
|
||||
}
|
||||
}
|
||||
|
||||
function App() {
|
||||
const [show,setShow] = React.useState(true);
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Hello, React!</h1>
|
||||
<ScrollList />
|
||||
<h1>Таймер с классом</h1>
|
||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
||||
{show && <Timer />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user