Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7607146ff1 |
+78
-76
@@ -1,96 +1,98 @@
|
|||||||
import React from 'react'
|
import { useState, useRef, useEffect } from 'react'
|
||||||
import styles from './App.module.scss'
|
|
||||||
|
|
||||||
class Clock extends React.Component {
|
function Example() {
|
||||||
constructor(props) {
|
const myRef = useRef(null);
|
||||||
super(props);
|
const counterRef = useRef(0);
|
||||||
this.state = { date: new Date() };
|
const [text, setText] = useState('');
|
||||||
}
|
|
||||||
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>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Clock2() {
|
useEffect(() => {
|
||||||
const [date, setDate] = React.useState(new Date());
|
myRef.current?.focus();
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
const timerId = setInterval(()=> setDate(new Date()),1000);
|
|
||||||
return () => clearInterval(timerId);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return <div>{date.toLocaleTimeString()}</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
function MouseTracker() {
|
|
||||||
const [x,setX] = React.useState(0);
|
|
||||||
const [y,setY] = React.useState(0);
|
|
||||||
|
|
||||||
const mouseMoveHandler = event => {
|
|
||||||
setX(event.clientX);
|
|
||||||
setY(event.clientY);
|
|
||||||
}
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
document.addEventListener('mousemove', mouseMoveHandler);
|
|
||||||
return () => {
|
|
||||||
document.removeEventListener('mousemove',mouseMoveHandler);
|
|
||||||
}
|
|
||||||
},[]);
|
},[]);
|
||||||
|
|
||||||
return <div className={styles.container}>
|
const handlerFocus = () => {
|
||||||
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
|
if(myRef.current) {
|
||||||
</div>
|
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 DataFetcher() {
|
function ClickCounter() {
|
||||||
const [posts,setPosts] = React.useState([]);
|
const countRef = useRef(0);
|
||||||
const [users,setUsers] = React.useState([]);
|
|
||||||
|
|
||||||
const getPosts = async () => {
|
function handlerClick() {
|
||||||
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
|
countRef.current += 1;
|
||||||
const data = await response.json();
|
//alert(`Нажали ${countRef.current} раз.`);
|
||||||
setUsers(data.reduce((acc, value) => {
|
}
|
||||||
if (!(value.userId in acc))
|
return <button onClick={handlerClick}>Нажать</button>;
|
||||||
return [...acc,value.userId]
|
}
|
||||||
return acc
|
|
||||||
}))
|
|
||||||
console.log(users);
|
|
||||||
setPosts(data.slice(1,10));
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
React.useEffect(() => {
|
return (
|
||||||
console.log("Компонент создан");
|
<>
|
||||||
getPosts();
|
<div ref={listRef}
|
||||||
return () => {console.log("Компонент удален");setPosts([])};
|
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
||||||
},[])
|
{[...Array(20)].map((_,i)=>(
|
||||||
|
<div key={i}>Элемент № {i + 1}</div>
|
||||||
return <div><ol>{posts.map(post => <li id={post.id}>{post.title}</li>)}</ol></div>
|
))}
|
||||||
|
</div>
|
||||||
|
<button onClick={scrollToBottom}>Прокрутить вниз</button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tracker,setTracker] = React.useState(false);
|
|
||||||
const [dataFetcher,setDataFetcher] = React.useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Clock />
|
<ScrollList />
|
||||||
<Clock2 />
|
|
||||||
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
|
||||||
{ tracker && <MouseTracker /> }
|
|
||||||
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
|
||||||
{ dataFetcher && <DataFetcher /> }
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
.container {
|
|
||||||
width: 300px;
|
|
||||||
height: 200px;
|
|
||||||
border: 2px dashed grey;
|
|
||||||
margin: 10px;
|
|
||||||
text-align: center;
|
|
||||||
p {
|
|
||||||
margin-top: 20%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
<StrictMode>
|
|
||||||
<App />
|
<App />
|
||||||
</StrictMode>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user