Compare commits

..

2 Commits

4 changed files with 86 additions and 100 deletions
-27
View File
@@ -8,7 +8,6 @@
"name": "store-client",
"version": "0.0.0",
"dependencies": {
"@tanstack/react-query": "^5.100.14",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
@@ -1305,32 +1304,6 @@
"win32"
]
},
"node_modules/@tanstack/query-core": {
"version": "5.100.14",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.100.14.tgz",
"integrity": "sha512-5X41dGpxgeaHISCRW2oYwcSycZeULZzAunaudXT9ov1KOTj9xwt0CH6hbwqP1/z74ZWF7rYFnDpyYH07XFcZew==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.100.14",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.100.14.tgz",
"integrity": "sha512-oOr6aRdSFEwWhzxEkD/9ZcItM3+LjBSkeVmadWKwUssAHTsqd/7bOjWrX4AbvEkoEhgAxzN0Xk6H/aYzXiYBAw==",
"license": "MIT",
"dependencies": {
"@tanstack/query-core": "5.100.14"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@types/babel__core": {
"version": "7.20.5",
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",
-1
View File
@@ -10,7 +10,6 @@
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "^5.100.14",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
+74 -71
View File
@@ -1,93 +1,96 @@
import React, {useState, useEffect, useRef } from 'react'
//import { useQuery } from '@tanstack/react-query'
import React from 'react'
import styles from './App.module.scss'
function useWindowWidth() {
const [width,setWidth] = useState(window.innerWidth)
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
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>
}
}
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener('resize',handleResize);
return () => window.removeEventListener('resize',handleResize);
function Clock2() {
const [date, setDate] = React.useState(new Date());
React.useEffect(()=>{
const timerId = setInterval(()=> setDate(new Date()),1000);
return () => clearInterval(timerId);
},[])
return width
return <div>{date.toLocaleTimeString()}</div>
}
function useLocalStorage(key, initValue) {
const [value, setValue] = useState(()=>{
const stored = localStorage.getItem(key);
return stored !== null ? JSON.parse(stored) : initValue
})
function MouseTracker() {
const [x,setX] = React.useState(0);
const [y,setY] = React.useState(0);
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value))
},[key, value])
return [value,setValue]
}
/*
function useUserData(userId) {
return useQuery(
['user',userId],
() => fetch(`/api/user/${userId}`).then(res => res.json()),
{refetchOnWindowFocus: false}
)
}*/
function useInput(initValue, validate) {
const [value, setValue] = useState(initValue);
const [error, setError] = useState(null);
const onChange = e => {
const newValue = e.target.value
setValue(newValue)
if (validate) {
const validationResult = validate(newValue)
setError(validationResult)
}
const mouseMoveHandler = event => {
setX(event.clientX);
setY(event.clientY);
}
return { value, error, onChange }
React.useEffect(()=>{
document.addEventListener('mousemove', mouseMoveHandler);
return () => {
document.removeEventListener('mousemove',mouseMoveHandler);
}
},[]);
return <div className={styles.container}>
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
</div>
}
function useInterval(callback, delay) {
const savedCallback = useRef()
function DataFetcher() {
const [posts,setPosts] = React.useState([]);
const [users,setUsers] = React.useState([]);
useEffect(()=>{
savedCallback.current = callback
},[callback]);
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));
useEffect(()=>{
if (delay == null) return
const id = setInterval(()=>{
savedCallback.current()
},delay);
return () => clearInterval(id);
},[delay])
}
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 width = useWindowWidth();
const [theme, setTheme] = useLocalStorage('theme','light');
const username = useInput(
'',
value => value.length < 3 ? 'минимум 3 символа' : null
)
const [tracker,setTracker] = React.useState(false);
const [dataFetcher,setDataFetcher] = React.useState(false);
return (
<>
<h1>Hello, React!{width}</h1>
<button onClick={()=>{
setTheme(theme === 'light' ? 'dark' : 'light')
}}>
Сменить тему
</button>
<p><input type="text"
value={username.value}
onChange={username.onChange} />
{ username.error ? username.error : '' }
</p>
<h1>Hello, React!</h1>
<Clock />
<Clock2 />
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
{ tracker && <MouseTracker /> }
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
{ dataFetcher && <DataFetcher /> }
</>
)
}
+11
View File
@@ -0,0 +1,11 @@
.container {
width: 300px;
height: 200px;
border: 2px dashed grey;
margin: 10px;
text-align: center;
p {
margin-top: 20%;
}
}