Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+78
-88
@@ -1,108 +1,98 @@
|
|||||||
import React from 'react'
|
import { useState, useRef, useEffect } from 'react'
|
||||||
|
|
||||||
class MyComponent extends React.Component {
|
function Example() {
|
||||||
constructor(props) {
|
const myRef = useRef(null);
|
||||||
super(props);
|
const counterRef = useRef(0);
|
||||||
this.state = { data: null };
|
const [text, setText] = useState('');
|
||||||
this.handleClick = this.handleClick.bind(this);
|
|
||||||
}
|
useEffect(() => {
|
||||||
|
myRef.current?.focus();
|
||||||
render() {
|
},[]);
|
||||||
return (
|
|
||||||
<div>
|
const handlerFocus = () => {
|
||||||
{this.state.data ? (
|
if(myRef.current) {
|
||||||
<div>Данные: {this.state.data}</div>
|
myRef.current.focus();
|
||||||
) : (
|
}
|
||||||
<div>Загрузка...</div>
|
};
|
||||||
)}
|
|
||||||
<button onClick={this.handleClick}>Обновить</button>
|
const handlerChange = (event) => {
|
||||||
</div>
|
setText(event.target.value);
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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: 'Данные'})
|
|
||||||
}
|
}
|
||||||
|
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>
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyComponent2 extends React.Component {
|
function ClickCounter() {
|
||||||
controller = new AbortController()
|
const countRef = useRef(0);
|
||||||
|
|
||||||
componentDidMount() {
|
function handlerClick() {
|
||||||
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
countRef.current += 1;
|
||||||
.then(responce => responce.json())
|
//alert(`Нажали ${countRef.current} раз.`);
|
||||||
.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()
|
|
||||||
}
|
}
|
||||||
|
return <button onClick={handlerClick}>Нажать</button>;
|
||||||
}
|
}
|
||||||
class MyComponent3 extends React.Component {
|
|
||||||
componentDidMount() {
|
|
||||||
this.loadData();
|
|
||||||
}
|
|
||||||
|
|
||||||
async loadData() {
|
function TimerComponent() {
|
||||||
try {
|
const timerIdRef = useRef(null);
|
||||||
const response = await fetch('https://api.example.com/data');
|
|
||||||
const data = await response.json();
|
useEffect(() => {
|
||||||
this.setSate({ data });
|
timerIdRef.current = setTimeout(() => {
|
||||||
} catch {
|
alert("Прошло 3 секунды");
|
||||||
console.log('error');
|
}, 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class Timer extends React.Component {
|
return (
|
||||||
constructor(props) {
|
<>
|
||||||
super(props);
|
<div ref={listRef}
|
||||||
this.state = { seconds: 0 }
|
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
||||||
}
|
{[...Array(20)].map((_,i)=>(
|
||||||
|
<div key={i}>Элемент № {i + 1}</div>
|
||||||
componentDidMount() {
|
))}
|
||||||
console.log('Timer mounted');
|
</div>
|
||||||
this.interval = setInterval(()=>{
|
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||||
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() {
|
function App() {
|
||||||
const [show,setShow] = React.useState(true);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Таймер с классом</h1>
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
<ScrollList />
|
||||||
{show && <Timer />}
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user