Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef33d16373 |
+50
-49
@@ -1,59 +1,60 @@
|
|||||||
import React from 'react'
|
import { memo, useState, useMemo } from 'react'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
function memoize(fn) {
|
||||||
constructor(props) {
|
const cache = new Map();
|
||||||
super(props)
|
return function(...args) {
|
||||||
this.state = { user: {} }
|
const key = JSON.stringify(args);
|
||||||
}
|
if (cache.has(key)) {
|
||||||
|
return cache.get(key);
|
||||||
componentDidMount() {
|
}
|
||||||
fetch('https://randomuser.me/api')
|
const result = fn.apply(this, args);
|
||||||
.then(responce => responce.json())
|
cache.set(key,result);
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
return result;
|
||||||
console.log('смонтирован')
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate(prevProps,prevState) {
|
|
||||||
console.log(this.state);
|
|
||||||
console.log('Компонент обнвлен')
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount() {
|
|
||||||
console.log('Компонент таймера размонтирован')
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return <div>
|
|
||||||
{this.state.user &&
|
|
||||||
this.state.user.name &&
|
|
||||||
(
|
|
||||||
<>
|
|
||||||
<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 fibonachi(n, memo={}) {
|
||||||
const [showClock, setShowClock] = React.useState(true);
|
if (n in memo) {console.log(n);return memo[n];}
|
||||||
|
if (n <= 1) return 1;
|
||||||
|
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
||||||
|
return memo[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
const MyComponent = ({ value }) => {
|
||||||
|
console.log('Rendring');
|
||||||
|
return <div>{value}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
const MemoizedComponent = memo(MyComponent);
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<>
|
||||||
|
<Child>
|
||||||
|
{stableChild}
|
||||||
|
</Child>
|
||||||
|
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
||||||
|
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Домашнее задание: жизненый цикл</h1>
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<Parent />
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
</>
|
||||||
</button>
|
|
||||||
{showClock && <RandomUser />}
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user