Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c168c96820 | |||
| b70fa47bbc | |||
| bd35029fc1 | |||
| 92dc5df96b | |||
| 3d34262203 | |||
| 1bc2e883a1 | |||
| 8a8a17f44d | |||
| 8f46470a26 |
Generated
+1256
-36
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -10,7 +10,7 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/react-query": "^5.100.14",
|
"info": "^1.0.0",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -23,6 +23,8 @@
|
|||||||
"eslint-plugin-react-hooks": "^5.2.0",
|
"eslint-plugin-react-hooks": "^5.2.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.19",
|
"eslint-plugin-react-refresh": "^0.4.19",
|
||||||
"globals": "^16.0.0",
|
"globals": "^16.0.0",
|
||||||
|
"sass": "^1.99.0",
|
||||||
|
"sass-embedded": "^1.99.0",
|
||||||
"vite": "^6.3.5"
|
"vite": "^6.3.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-80
@@ -1,93 +1,29 @@
|
|||||||
import React, {useState, useEffect, useRef } from 'react'
|
import styles from './styles/App.module.scss'
|
||||||
//import { useQuery } from '@tanstack/react-query'
|
import React, { useState, useCallback } from 'react'
|
||||||
|
|
||||||
function useWindowWidth() {
|
function CounterButton({label, onClick}) {
|
||||||
const [width,setWidth] = useState(window.innerWidth)
|
return (
|
||||||
|
<button onClick={onClick} className={styles.counter_button}>{label}</button>
|
||||||
useEffect(() => {
|
|
||||||
const handleResize = () => setWidth(window.innerWidth);
|
|
||||||
window.addEventListener('resize',handleResize);
|
|
||||||
return () => window.removeEventListener('resize',handleResize);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return width
|
|
||||||
}
|
|
||||||
|
|
||||||
function useLocalStorage(key, initValue) {
|
|
||||||
const [value, setValue] = useState(()=>{
|
|
||||||
const stored = localStorage.getItem(key);
|
|
||||||
return stored !== null ? JSON.parse(stored) : initValue
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { value, error, onChange }
|
|
||||||
}
|
}
|
||||||
|
function CounterDisplay({value}) {
|
||||||
function useInterval(callback, delay) {
|
return (<div className={styles.counter_display}>
|
||||||
const savedCallback = useRef()
|
Значение счетчика: <b>{value}</b>
|
||||||
|
</div>)
|
||||||
useEffect(()=>{
|
|
||||||
savedCallback.current = callback
|
|
||||||
},[callback]);
|
|
||||||
|
|
||||||
useEffect(()=>{
|
|
||||||
if (delay == null) return
|
|
||||||
const id = setInterval(()=>{
|
|
||||||
savedCallback.current()
|
|
||||||
},delay);
|
|
||||||
return () => clearInterval(id);
|
|
||||||
},[delay])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const width = useWindowWidth();
|
const [counter,setCounter] = useState(0);
|
||||||
const [theme, setTheme] = useLocalStorage('theme','light');
|
|
||||||
|
|
||||||
const username = useInput(
|
const increment = () => setCounter(prev => prev + 1);
|
||||||
'',
|
const decrement = () => setCounter(prev => prev - 1);
|
||||||
value => value.length < 3 ? 'минимум 3 символа' : null
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!{width}</h1>
|
<h2>Практика по созданию счетчика с колбэками</h2>
|
||||||
<button onClick={()=>{
|
<CounterButton label="Увеличить" onClick={increment} />
|
||||||
setTheme(theme === 'light' ? 'dark' : 'light')
|
<CounterButton label="уменьшить" onClick={decrement} />
|
||||||
}}>
|
<CounterDisplay value={counter} />
|
||||||
Сменить тему
|
|
||||||
</button>
|
|
||||||
<p><input type="text"
|
|
||||||
value={username.value}
|
|
||||||
onChange={username.onChange} />
|
|
||||||
{ username.error ? username.error : '' }
|
|
||||||
</p>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { StrictMode } from 'react'
|
import { StrictMode } from 'react'
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from 'react-dom/client'
|
||||||
// удаляем import './index.css'
|
|
||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
.container {
|
||||||
|
display:flex;
|
||||||
|
flex-direction: row;
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
p {
|
||||||
|
padding: 5px 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.counter {
|
||||||
|
&_display{
|
||||||
|
border: 1px solid black;
|
||||||
|
border-width: 3px;
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 10px;
|
||||||
|
width: 300px;
|
||||||
|
}
|
||||||
|
&_button{
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user