Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as ef33d16373 Сделали React.memo и React.useMemo 2026-05-21 21:33:02 +03:00
+44 -93
View File
@@ -1,108 +1,59 @@
import React from 'react' import { memo, useState, useMemo } from 'react'
class MyComponent extends React.Component { function memoize(fn) {
constructor(props) { const cache = new Map();
super(props); return function(...args) {
this.state = { data: null }; const key = JSON.stringify(args);
this.handleClick = this.handleClick.bind(this); if (cache.has(key)) {
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: 'Данные'})
} }
} }
class MyComponent2 extends React.Component { function fibonachi(n, memo={}) {
controller = new AbortController() if (n in memo) {console.log(n);return memo[n];}
if (n <= 1) return 1;
componentDidMount() { memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
fetch('https://api.example.com/data',{ singnal: this.controller.signal }) return memo[n];
.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 { const MyComponent = ({ value }) => {
constructor(props) { console.log('Rendring');
super(props); return <div>{value}</div>
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() { const MemoizedComponent = memo(MyComponent);
const [show,setShow] = React.useState(true);
const Child = memo(({ children }) => {
console.log('Child component rendred');
return <div>{children}</div>;
});
const Parent = () => {
const [count,setCount] = useState(0);
const [text, setText] = useState('');
const stableChild = useMemo(() => <span>Memo text</span>,[]);
return ( return (
<> <>
<h1>Таймер с классом</h1> <Child>
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button> {stableChild}
{show && <Timer />} </Child>
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
</>
)
}
function App() {
return (
<>
<h1>Hello, React!</h1>
<Parent />
</> </>
) )
} }