Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ee1bf2eee | |||
| c137c644e3 |
+27
-91
@@ -1,108 +1,44 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
class MyComponent extends React.Component {
|
class Clock extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props)
|
||||||
this.state = { data: null };
|
this.state = { time: new Date().toLocaleTimeString() }
|
||||||
this.handleClick = this.handleClick.bind(this);
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.timerId = setInterval(()=>{
|
||||||
|
this.setState({ time: new Date().toLocaleTimeString() })
|
||||||
|
}, 1000);
|
||||||
|
console.log('Таймер смонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(prevProps,prevState) {
|
||||||
|
console.log('Компонент обнвлен')
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
clearInterval(this.timerId)
|
||||||
|
console.log('Компонент таймера размонтирован')
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return <div>
|
||||||
<div>
|
<h2>Текущее время: {this.state.time}</h2>
|
||||||
{this.state.data ? (
|
|
||||||
<div>Данные: {this.state.data}</div>
|
|
||||||
) : (
|
|
||||||
<div>Загрузка...</div>
|
|
||||||
)}
|
|
||||||
<button onClick={this.handleClick}>Обновить</button>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
function App() {
|
||||||
const [show,setShow] = React.useState(true);
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Таймер с классом</h1>
|
<h1>Жизненный цикл компонента часов</h1>
|
||||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
{show && <Timer />}
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
|
</button>
|
||||||
|
{showClock && <Clock />}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user