Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8cc72afd7b | |||
| a520406547 | |||
| 38f7195ed4 |
+29
-82
@@ -1,98 +1,45 @@
|
|||||||
import { useState, useRef, useEffect } from 'react'
|
import React, { useMemo, useCallback, useState } from 'react'
|
||||||
|
|
||||||
function Example() {
|
const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
|
||||||
const myRef = useRef(null);
|
console.log(`rendred ${item.id}`);
|
||||||
const counterRef = useRef(0);
|
return <li>
|
||||||
const [text, setText] = useState('');
|
{item.name}
|
||||||
|
<button onClick={() => onToggle(item.id)}>Done</button>
|
||||||
useEffect(() => {
|
<button onClick={() => onDelete(item.id)}>remove</button>
|
||||||
myRef.current?.focus();
|
</li>
|
||||||
|
})
|
||||||
|
|
||||||
|
function ItemList({ items }) {
|
||||||
|
|
||||||
|
const handleToggle = useCallback(id => {
|
||||||
|
items.done = true;
|
||||||
|
console.log(`Item ${id} done`)
|
||||||
|
console.log(id)
|
||||||
},[]);
|
},[]);
|
||||||
|
|
||||||
const handlerFocus = () => {
|
|
||||||
if(myRef.current) {
|
|
||||||
myRef.current.focus();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlerChange = (event) => {
|
const handleDelete = useCallback(id => {
|
||||||
setText(event.target.value);
|
items.pop(id);
|
||||||
}
|
|
||||||
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 (
|
|
||||||
<div>
|
|
||||||
Текущее значение: {value} <br />
|
|
||||||
Предыдущее значение: {prevValueRef.current}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function ScrollList() {
|
|
||||||
const listRef = useRef(null);
|
|
||||||
|
|
||||||
const scrollToBottom = () => {
|
|
||||||
if(listRef.current) {
|
|
||||||
listRef.current.scrollTop = listRef.current.scrollHeight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div ref={listRef}
|
<h3>List items</h3>
|
||||||
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
{items.map(item => (
|
||||||
{[...Array(20)].map((_,i)=>(
|
<TodoItem key={item.id} item={item}
|
||||||
<div key={i}>Элемент № {i + 1}</div>
|
onToggle={handleToggle} onDelete={handleDelete} />
|
||||||
))}
|
))}
|
||||||
</div>
|
|
||||||
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<ScrollList />
|
<ItemList items={[
|
||||||
|
{id:1,name:'Первый',done:false},
|
||||||
|
{id:2,name:'Второй',done:false}
|
||||||
|
]} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user