Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12515f925d | |||
| b3662b759f |
+98
-35
@@ -1,45 +1,108 @@
|
|||||||
import React, { useMemo, useCallback, useState } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
|
class MyComponent extends React.Component {
|
||||||
console.log(`rendred ${item.id}`);
|
constructor(props) {
|
||||||
return <li>
|
super(props);
|
||||||
{item.name}
|
this.state = { data: null };
|
||||||
<button onClick={() => onToggle(item.id)}>Done</button>
|
this.handleClick = this.handleClick.bind(this);
|
||||||
<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)
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
const handleDelete = useCallback(id => {
|
render() {
|
||||||
items.pop(id);
|
return (
|
||||||
},[]);
|
<div>
|
||||||
return (
|
{this.state.data ? (
|
||||||
<>
|
<div>Данные: {this.state.data}</div>
|
||||||
<h3>List items</h3>
|
) : (
|
||||||
{items.map(item => (
|
<div>Загрузка...</div>
|
||||||
<TodoItem key={item.id} item={item}
|
)}
|
||||||
onToggle={handleToggle} onDelete={handleDelete} />
|
<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 {
|
||||||
|
controller = new AbortController()
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({data: json}))
|
||||||
|
.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() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('https://api.example.com/data');
|
||||||
|
const data = await response.json();
|
||||||
|
this.setSate({ data });
|
||||||
|
} catch {
|
||||||
|
console.log('error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
<h1>Hello, React!</h1>
|
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
||||||
<ItemList items={[
|
{show && <Timer />}
|
||||||
{id:1,name:'Первый',done:false},
|
|
||||||
{id:2,name:'Второй',done:false}
|
|
||||||
]} />
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user