Сделали компонент с фотографией
This commit is contained in:
+24
-10
@@ -1,30 +1,44 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
class Clock extends React.Component {
|
class RandomUser extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = { time: new Date().toLocaleTimeString() }
|
this.state = { user: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.timerId = setInterval(()=>{
|
fetch('https://randomuser.me/api')
|
||||||
this.setState({ time: new Date().toLocaleTimeString() })
|
.then(responce => responce.json())
|
||||||
}, 1000);
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
console.log('Таймер смонтирован')
|
console.log('смонтирован')
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps,prevState) {
|
componentDidUpdate(prevProps,prevState) {
|
||||||
|
console.log(this.state);
|
||||||
console.log('Компонент обнвлен')
|
console.log('Компонент обнвлен')
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
clearInterval(this.timerId)
|
|
||||||
console.log('Компонент таймера размонтирован')
|
console.log('Компонент таймера размонтирован')
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div>
|
return <div>
|
||||||
<h2>Текущее время: {this.state.time}</h2>
|
{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>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,11 +48,11 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Жизненный цикл компонента часов</h1>
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
</button>
|
</button>
|
||||||
{showClock && <Clock />}
|
{showClock && <RandomUser />}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user