Compare commits

..

3 Commits

+46 -33
View File
@@ -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() {
<h3>List items</h3> return <div>
{items.map(item => ( {this.state.user &&
<TodoItem key={item.id} item={item} this.state.user.name &&
onToggle={handleToggle} onDelete={handleDelete} /> (
))} <>
</> <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() { 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 />}
]} />
</> </>
) )
} }