|
|
|
@@ -1,98 +1,96 @@
|
|
|
|
|
import { useState, useRef, useEffect } from 'react'
|
|
|
|
|
import React from 'react'
|
|
|
|
|
import styles from './App.module.scss'
|
|
|
|
|
|
|
|
|
|
function Example() {
|
|
|
|
|
const myRef = useRef(null);
|
|
|
|
|
const counterRef = useRef(0);
|
|
|
|
|
const [text, setText] = useState('');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
myRef.current?.focus();
|
|
|
|
|
},[]);
|
|
|
|
|
|
|
|
|
|
const handlerFocus = () => {
|
|
|
|
|
if(myRef.current) {
|
|
|
|
|
myRef.current.focus();
|
|
|
|
|
class Clock extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = { date: new Date() };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handlerChange = (event) => {
|
|
|
|
|
setText(event.target.value);
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.timerId = setInterval( () => this.tick(), 1000);
|
|
|
|
|
}
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
clearInterval(this.timerId)
|
|
|
|
|
}
|
|
|
|
|
tick() {
|
|
|
|
|
this.setState({ date: new Date() })
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return <div>{this.state.date.toLocaleTimeString()}</div>
|
|
|
|
|
}
|
|
|
|
|
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 Clock2() {
|
|
|
|
|
const [date, setDate] = React.useState(new Date());
|
|
|
|
|
|
|
|
|
|
function handlerClick() {
|
|
|
|
|
countRef.current += 1;
|
|
|
|
|
//alert(`Нажали ${countRef.current} раз.`);
|
|
|
|
|
}
|
|
|
|
|
return <button onClick={handlerClick}>Нажать</button>;
|
|
|
|
|
React.useEffect(()=>{
|
|
|
|
|
const timerId = setInterval(()=> setDate(new Date()),1000);
|
|
|
|
|
return () => clearInterval(timerId);
|
|
|
|
|
},[])
|
|
|
|
|
|
|
|
|
|
return <div>{date.toLocaleTimeString()}</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function TimerComponent() {
|
|
|
|
|
const timerIdRef = useRef(null);
|
|
|
|
|
function MouseTracker() {
|
|
|
|
|
const [x,setX] = React.useState(0);
|
|
|
|
|
const [y,setY] = React.useState(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
timerIdRef.current = setTimeout(() => {
|
|
|
|
|
alert("Прошло 3 секунды");
|
|
|
|
|
}, 3000);
|
|
|
|
|
const mouseMoveHandler = event => {
|
|
|
|
|
setX(event.clientX);
|
|
|
|
|
setY(event.clientY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
React.useEffect(()=>{
|
|
|
|
|
document.addEventListener('mousemove', mouseMoveHandler);
|
|
|
|
|
return () => {
|
|
|
|
|
clearTimeout(timerIdRef.current);
|
|
|
|
|
document.removeEventListener('mousemove',mouseMoveHandler);
|
|
|
|
|
}
|
|
|
|
|
},[]);
|
|
|
|
|
|
|
|
|
|
return <div className={styles.container}>
|
|
|
|
|
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function PrevValue({ value }) {
|
|
|
|
|
const prevValueRef = useRef();
|
|
|
|
|
function DataFetcher() {
|
|
|
|
|
const [posts,setPosts] = React.useState([]);
|
|
|
|
|
const [users,setUsers] = React.useState([]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
prevValueRef.current = value;
|
|
|
|
|
});
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
Текущее значение: {value} <br />
|
|
|
|
|
Предыдущее значение: {prevValueRef.current}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
const getPosts = async () => {
|
|
|
|
|
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
setUsers(data.reduce((acc, value) => {
|
|
|
|
|
if (!(value.userId in acc))
|
|
|
|
|
return [...acc,value.userId]
|
|
|
|
|
return acc
|
|
|
|
|
}))
|
|
|
|
|
console.log(users);
|
|
|
|
|
setPosts(data.slice(1,10));
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
console.log("Компонент создан");
|
|
|
|
|
getPosts();
|
|
|
|
|
return () => {console.log("Компонент удален");setPosts([])};
|
|
|
|
|
},[])
|
|
|
|
|
|
|
|
|
|
return <div><ol>{posts.map(post => <li id={post.id}>{post.title}</li>)}</ol></div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
|
|
|
|
|
const [tracker,setTracker] = React.useState(false);
|
|
|
|
|
const [dataFetcher,setDataFetcher] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<h1>Hello, React!</h1>
|
|
|
|
|
<ScrollList />
|
|
|
|
|
<Clock />
|
|
|
|
|
<Clock2 />
|
|
|
|
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
|
|
|
|
{ tracker && <MouseTracker /> }
|
|
|
|
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
|
|
|
|
{ dataFetcher && <DataFetcher /> }
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|