Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+78
-62
@@ -1,82 +1,98 @@
|
||||
import memo, { useMemo, useCallback, useState } from 'react'
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
|
||||
function MyComponent() {
|
||||
const [count, setCount] = useState(0);
|
||||
const [name, setName] = useState('John');
|
||||
function Example() {
|
||||
const myRef = useRef(null);
|
||||
const counterRef = useRef(0);
|
||||
const [text, setText] = useState('');
|
||||
|
||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
||||
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 (
|
||||
<>
|
||||
<h2>{greeting}</h2>
|
||||
<button onClick={()=>setName('Alex')}>New name</button>
|
||||
</>
|
||||
<div>
|
||||
Текущее значение: {value} <br />
|
||||
Предыдущее значение: {prevValueRef.current}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function MyComponent2() {
|
||||
const [data,setData] = useState([{theme:'light'}])
|
||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
||||
function ScrollList() {
|
||||
const listRef = useRef(null);
|
||||
|
||||
const processData = useMemo(()=>{
|
||||
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
||||
},[data, userPreferences]);
|
||||
console.log(processData);
|
||||
const scrollToBottom = () => {
|
||||
if(listRef.current) {
|
||||
listRef.current.scrollTop = listRef.current.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2 className={data.theme}>2</h2>
|
||||
<button onClick={()=>setData(data)}>set new theme</button>
|
||||
<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 MyComponent3() {
|
||||
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
||||
|
||||
//Применение оправдано
|
||||
const complexResult = useMemo(()=>{
|
||||
return heavyMethOperations(largeDataSet);
|
||||
},[largeDataSet])
|
||||
}
|
||||
|
||||
function ParentComponent({ items }) {
|
||||
const [filter, setFilter] = useState('');
|
||||
|
||||
const listProps = useMemo(()=>({
|
||||
items: items.filter(item => item.name.includes(filter)),
|
||||
onItemClick: id => console.log('Clicked',id),
|
||||
theme: 'dark'
|
||||
}),[items,filter]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Memoized {...listProps} />
|
||||
<button onClick={()=>setFilter('Bob')}>Click</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function Memoized(props) {
|
||||
console.log(props);
|
||||
return (
|
||||
<>
|
||||
<h3 className={props.theme}>Memoized</h3>
|
||||
{props.items.map((item) => (
|
||||
<>
|
||||
<li>
|
||||
{item.name}
|
||||
</li>
|
||||
</>))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
function App() {
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<h1>Hello, React!</h1>
|
||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
||||
<ScrollList />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user