Compare commits

..

3 Commits

Author SHA1 Message Date
laktionov-as 8cc72afd7b final 2026-05-28 19:16:14 +03:00
laktionov-as a520406547 useCallback with React.memo 2026-05-26 21:28:43 +03:00
laktionov-as 38f7195ed4 useCallback 2026-05-26 21:17:42 +03:00
+33 -96
View File
@@ -1,108 +1,45 @@
import React from 'react' import React, { useMemo, useCallback, useState } from 'react'
class MyComponent extends React.Component { const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
constructor(props) { console.log(`rendred ${item.id}`);
super(props); return <li>
this.state = { data: null }; {item.name}
this.handleClick = this.handleClick.bind(this); <button onClick={() => onToggle(item.id)}>Done</button>
} <button onClick={() => onDelete(item.id)}>remove</button>
</li>
render() { })
return (
<div>
{this.state.data ? (
<div>Данные: {this.state.data}</div>
) : (
<div>Загрузка...</div>
)}
<button onClick={this.handleClick}>Обновить</button>
</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 { function ItemList({ items }) {
controller = new AbortController()
componentDidMount() { const handleToggle = useCallback(id => {
fetch('https://api.example.com/data',{ singnal: this.controller.signal }) items.done = true;
.then(responce => responce.json()) console.log(`Item ${id} done`)
.then(json => this.setState({data: json})) console.log(id)
.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() { const handleDelete = useCallback(id => {
try { items.pop(id);
const response = await fetch('https://api.example.com/data'); },[]);
const data = await response.json(); return (
this.setSate({ data }); <>
} catch { <h3>List items</h3>
console.log('error'); {items.map(item => (
} <TodoItem key={item.id} item={item}
} onToggle={handleToggle} onDelete={handleDelete} />
} ))}
</>
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);
return ( return (
<> <>
<h1>Таймер с классом</h1> {/* Добавьте тестовый контент для проверки */}
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button> <h1>Hello, React!</h1>
{show && <Timer />} <ItemList items={[
{id:1,name:'Первый',done:false},
{id:2,name:'Второй',done:false}
]} />
</> </>
) )
} }