Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+84
-45
@@ -1,60 +1,99 @@
|
|||||||
import { memo, useState, useMemo } from 'react'
|
import { useState, useRef, useEffect } from 'react'
|
||||||
|
|
||||||
function memoize(fn) {
|
function Example() {
|
||||||
const cache = new Map();
|
const myRef = useRef(null);
|
||||||
return function(...args) {
|
const counterRef = useRef(0);
|
||||||
const key = JSON.stringify(args);
|
|
||||||
if (cache.has(key)) {
|
|
||||||
return cache.get(key);
|
|
||||||
}
|
|
||||||
const result = fn.apply(this, args);
|
|
||||||
cache.set(key,result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function fibonachi(n, memo={}) {
|
|
||||||
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 [text, setText] = useState('');
|
||||||
|
|
||||||
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
useEffect(() => {
|
||||||
|
myRef.current?.focus();
|
||||||
|
},[]);
|
||||||
|
|
||||||
|
const handlerFocus = () => {
|
||||||
|
if(myRef.current) {
|
||||||
|
myRef.current.focus();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlerChange = (event) => {
|
||||||
|
setText(event.target.value);
|
||||||
|
}
|
||||||
|
counterRef.current += 1;
|
||||||
|
return <div>
|
||||||
|
<input ref={myRef} text="text" /><br />
|
||||||
|
Количество рендров: {counterRef.current}<br />
|
||||||
|
<input text="text" onChange={handlerChange} value={text} />
|
||||||
|
<button onClick={handlerFocus}>Поставить фокус на поле</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
function ClickCounter() {
|
||||||
|
const countRef = useRef(0);
|
||||||
|
|
||||||
|
function handlerClick() {
|
||||||
|
countRef.current += 1;
|
||||||
|
//alert(`Нажали ${countRef.current} раз.`);
|
||||||
|
}
|
||||||
|
return <button onClick={handlerClick}>Нажать</button>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function TimerComponent() {
|
||||||
|
const timerIdRef = useRef(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
timerIdRef.current = setTimeout(() => {
|
||||||
|
alert("Прошло 3 секунды");
|
||||||
|
}, 3000);
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timerIdRef.current);
|
||||||
|
}
|
||||||
|
},[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PrevValue({ value }) {
|
||||||
|
const prevValueRef = useRef();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
prevValueRef.current = value;
|
||||||
|
});
|
||||||
return (
|
return (
|
||||||
<>
|
<div>
|
||||||
<Child>
|
Текущее значение: {value} <br />
|
||||||
{stableChild}
|
Предыдущее значение: {prevValueRef.current}
|
||||||
</Child>
|
</div>
|
||||||
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
|
||||||
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ScrollList() {
|
||||||
|
const listRef = useRef(null);
|
||||||
|
|
||||||
|
const scrollToBottom = () => {
|
||||||
|
if(listRef.current) {
|
||||||
|
listRef.current.scrollTop = listRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div ref={listRef}
|
||||||
|
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
||||||
|
{[...Array(20)].map((_,i)=>(
|
||||||
|
<div key={i}>Элемент № {i + 1}</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Parent />
|
<ScrollList />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user