Compare commits

..

8 Commits

6 changed files with 1303 additions and 93 deletions
+1256 -9
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -10,6 +10,7 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"info": "^1.0.0",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0"
}, },
@@ -22,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 -65
View File
@@ -1,78 +1,29 @@
import { useState, createContext, useContext } from 'react' import styles from './styles/App.module.scss'
import styles from './App.module.scss' import React, { useState, useCallback } from 'react'
const UserContext = createContext('')
function UserProvider({ children }) {
const [name,setName] = useState('');
const toggleName = (newName) => {
setName(newName);
}
function CounterButton({label, onClick}) {
return ( return (
<UserContext.Provider value={{name, toggleName}}> <button onClick={onClick} className={styles.counter_button}>{label}</button>
{children}
</UserContext.Provider>
) )
} }
function CounterDisplay({value}) {
function Form() { return (<div className={styles.counter_display}>
const {name, toggleName} = useContext(UserContext) Значение счетчика: <b>{value}</b>
const [newName,setNewName] = useState(''); </div>)
const handleSubmit = (event) => {
event.preventDefault();
toggleName(event.target[0].value)
}
const handlerChange = (event) => {
setNewName(event.target.value);
}
return (
<form onSubmit={handleSubmit}>
<input type='text' value={newName} onChange={handlerChange} />
<button type='submit'>Войти</button>
</form>
)
}
function ExitButton() {
const {name, toggleName} = useContext(UserContext)
return (
<button onClick={() => toggleName('')}>Выход</button>
)
}
function Toolbar() {
const {name, toggleName} = useContext(UserContext)
return(
<div className={styles.container}>
<h3>Панель пользователя</h3>
{name && `Привет ${name}`}
{name ? <ExitButton /> : <Form /> }
</div>
)
}
function Page() {
return (
<div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
<Toolbar />
</div>
)
} }
function App() { function App() {
const [counter,setCounter] = useState(0);
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
return ( return (
<> <>
{/* Добавьте тестовый контент для проверки */} <h2>Практика по созданию счетчика с колбэками</h2>
<h1>Hello, React!</h1> <CounterButton label="Увеличить" onClick={increment} />
<UserProvider> <CounterButton label="уменьшить" onClick={decrement} />
<Page /> <CounterDisplay value={counter} />
</UserProvider>
</> </>
) )
} }
-13
View File
@@ -1,13 +0,0 @@
.container {
height: 500px;
border: 1px solid black;
border-radius: 10px;
background-color: lightblue;
}
.item {
font-size: 16pt;
border: 1px dotted gray;
/*margin: 5px 10px;
padding: 10px;*/
border-radius: 5px;
}
+1 -1
View File
@@ -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(
+22
View File
@@ -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;
}
}