Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2775fc8283 | |||
| c9759fbfc7 |
+68
-70
@@ -1,98 +1,96 @@
|
|||||||
import { useState, useRef, useEffect } from 'react'
|
import React from 'react'
|
||||||
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
function Example() {
|
class Clock extends React.Component {
|
||||||
const myRef = useRef(null);
|
constructor(props) {
|
||||||
const counterRef = useRef(0);
|
super(props);
|
||||||
const [text, setText] = useState('');
|
this.state = { date: new Date() };
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
myRef.current?.focus();
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
const handlerFocus = () => {
|
|
||||||
if(myRef.current) {
|
|
||||||
myRef.current.focus();
|
|
||||||
}
|
}
|
||||||
};
|
componentDidMount() {
|
||||||
|
this.timerId = setInterval( () => this.tick(), 1000);
|
||||||
const handlerChange = (event) => {
|
}
|
||||||
setText(event.target.value);
|
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() {
|
function Clock2() {
|
||||||
const countRef = useRef(0);
|
const [date, setDate] = React.useState(new Date());
|
||||||
|
|
||||||
function handlerClick() {
|
React.useEffect(()=>{
|
||||||
countRef.current += 1;
|
const timerId = setInterval(()=> setDate(new Date()),1000);
|
||||||
//alert(`Нажали ${countRef.current} раз.`);
|
return () => clearInterval(timerId);
|
||||||
}
|
},[])
|
||||||
return <button onClick={handlerClick}>Нажать</button>;
|
|
||||||
|
return <div>{date.toLocaleTimeString()}</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
function TimerComponent() {
|
function MouseTracker() {
|
||||||
const timerIdRef = useRef(null);
|
const [x,setX] = React.useState(0);
|
||||||
|
const [y,setY] = React.useState(0);
|
||||||
|
|
||||||
useEffect(() => {
|
const mouseMoveHandler = event => {
|
||||||
timerIdRef.current = setTimeout(() => {
|
setX(event.clientX);
|
||||||
alert("Прошло 3 секунды");
|
setY(event.clientY);
|
||||||
}, 3000);
|
}
|
||||||
|
|
||||||
|
React.useEffect(()=>{
|
||||||
|
document.addEventListener('mousemove', mouseMoveHandler);
|
||||||
return () => {
|
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 }) {
|
function DataFetcher() {
|
||||||
const prevValueRef = useRef();
|
const [posts,setPosts] = React.useState([]);
|
||||||
|
const [users,setUsers] = React.useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
const getPosts = async () => {
|
||||||
prevValueRef.current = value;
|
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
|
||||||
});
|
const data = await response.json();
|
||||||
return (
|
setUsers(data.reduce((acc, value) => {
|
||||||
<div>
|
if (!(value.userId in acc))
|
||||||
Текущее значение: {value} <br />
|
return [...acc,value.userId]
|
||||||
Предыдущее значение: {prevValueRef.current}
|
return acc
|
||||||
</div>
|
}))
|
||||||
)
|
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 (
|
React.useEffect(() => {
|
||||||
<>
|
console.log("Компонент создан");
|
||||||
<div ref={listRef}
|
getPosts();
|
||||||
style={{height: 200, overflowY: 'auto', border: '1px solid black'}}>
|
return () => {console.log("Компонент удален");setPosts([])};
|
||||||
{[...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>
|
||||||
<ScrollList />
|
<Clock />
|
||||||
|
<Clock2 />
|
||||||
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
||||||
|
{ tracker && <MouseTracker /> }
|
||||||
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
||||||
|
{ dataFetcher && <DataFetcher /> }
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.container {
|
||||||
|
width: 300px;
|
||||||
|
height: 200px;
|
||||||
|
border: 2px dashed grey;
|
||||||
|
margin: 10px;
|
||||||
|
text-align: center;
|
||||||
|
p {
|
||||||
|
margin-top: 20%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -4,5 +4,7 @@ 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