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