Compare commits

..

2 Commits

Author SHA1 Message Date
laktionov-as 12515f925d Сделали таймер 2026-05-28 20:45:57 +03:00
laktionov-as b3662b759f class components 2026-05-28 20:29:26 +03:00
+93 -44
View File
@@ -1,60 +1,109 @@
import { memo, useState, useMemo } from 'react' import React from 'react'
function memoize(fn) { class MyComponent extends React.Component {
const cache = new Map(); constructor(props) {
return function(...args) { super(props);
const key = JSON.stringify(args); this.state = { data: null };
if (cache.has(key)) { this.handleClick = this.handleClick.bind(this);
return cache.get(key); }
}
const result = fn.apply(this, args); render() {
cache.set(key,result); return (
return result; <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: 'Данные'})
} }
} }
function fibonachi(n, memo={}) { class MyComponent2 extends React.Component {
if (n in memo) {console.log(n);return memo[n];} controller = new AbortController()
if (n <= 1) return 1;
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo); componentDidMount() {
return memo[n]; 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');
}
}
} }
const MyComponent = ({ value }) => { class Timer extends React.Component {
console.log('Rendring'); constructor(props) {
return <div>{value}</div> super(props);
} this.state = { seconds: 0 }
}
const MemoizedComponent = memo(MyComponent); componentDidMount() {
console.log('Timer mounted');
this.interval = setInterval(()=>{
this.setState(prevState => ({seconds: prevState.seconds + 1}))
},1000);
}
const Child = memo(({ children }) => { componentWillUnmount() {
console.log('Child component rendred'); console.log('Timer unmount');
return <div>{children}</div>; clearInterval(this.interval);
}); }
render() {
const Parent = () => { return <h2>Прошло секунд: {this.state.seconds}</h2>
const [count,setCount] = useState(0); }
const [text, setText] = useState('');
const stableChild = useMemo(() => <span>Memo text</span>,[]);
return (
<>
<Child>
{stableChild}
</Child>
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
</>
)
} }
function App() { function App() {
const [show,setShow] = React.useState(true);
return ( return (
<> <>
<h1>Hello, React!</h1> <h1>Таймер с классом</h1>
<Parent /> <button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
</> {show && <Timer />}
</>
) )
} }