Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6827c800f1 | |||
| 5ee1bf2eee | |||
| c137c644e3 |
+43
-30
@@ -1,45 +1,58 @@
|
|||||||
import React, { useMemo, useCallback, useState } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
|
class RandomUser extends React.Component {
|
||||||
console.log(`rendred ${item.id}`);
|
constructor(props) {
|
||||||
return <li>
|
super(props)
|
||||||
{item.name}
|
this.state = { user: {} }
|
||||||
<button onClick={() => onToggle(item.id)}>Done</button>
|
}
|
||||||
<button onClick={() => onDelete(item.id)}>remove</button>
|
|
||||||
</li>
|
|
||||||
})
|
|
||||||
|
|
||||||
function ItemList({ items }) {
|
componentDidMount() {
|
||||||
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
console.log('смонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
const handleToggle = useCallback(id => {
|
componentDidUpdate(prevProps,prevState) {
|
||||||
items.done = true;
|
console.log(this.state);
|
||||||
console.log(`Item ${id} done`)
|
console.log('Компонент обнвлен')
|
||||||
console.log(id)
|
}
|
||||||
},[]);
|
|
||||||
|
|
||||||
const handleDelete = useCallback(id => {
|
componentWillUnmount() {
|
||||||
items.pop(id);
|
console.log('Компонент таймера размонтирован')
|
||||||
},[]);
|
}
|
||||||
return (
|
|
||||||
|
render() {
|
||||||
|
return <div>
|
||||||
|
{this.state.user &&
|
||||||
|
this.state.user.name &&
|
||||||
|
(
|
||||||
<>
|
<>
|
||||||
<h3>List items</h3>
|
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
||||||
{items.map(item => (
|
<img src={this.state.user.picture.medium} />
|
||||||
<TodoItem key={item.id} item={item}
|
<button onClick={() => {
|
||||||
onToggle={handleToggle} onDelete={handleDelete} />
|
console.log('Загружаем ... ')
|
||||||
))}
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
}}>Загрузить нового</button>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<h1>Hello, React!</h1>
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
<ItemList items={[
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
{id:1,name:'Первый',done:false},
|
</button>
|
||||||
{id:2,name:'Второй',done:false}
|
{showClock && <RandomUser />}
|
||||||
]} />
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user