Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
Generated
+9
-1256
File diff suppressed because it is too large
Load Diff
+1
-4
@@ -4,13 +4,12 @@
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"lint": "eslint .",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"info": "^1.0.0",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
},
|
||||
@@ -23,8 +22,6 @@
|
||||
"eslint-plugin-react-hooks": "^5.2.0",
|
||||
"eslint-plugin-react-refresh": "^0.4.19",
|
||||
"globals": "^16.0.0",
|
||||
"sass": "^1.99.0",
|
||||
"sass-embedded": "^1.99.0",
|
||||
"vite": "^6.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
+65
-16
@@ -1,29 +1,78 @@
|
||||
import styles from './styles/App.module.scss'
|
||||
import React, { useState, useCallback } from 'react'
|
||||
import { useState, createContext, useContext } from 'react'
|
||||
import styles from './App.module.scss'
|
||||
|
||||
function CounterButton({label, onClick}) {
|
||||
const UserContext = createContext('')
|
||||
|
||||
function UserProvider({ children }) {
|
||||
const [name,setName] = useState('');
|
||||
|
||||
const toggleName = (newName) => {
|
||||
setName(newName);
|
||||
}
|
||||
|
||||
return (
|
||||
<button onClick={onClick} className={styles.counter_button}>{label}</button>
|
||||
<UserContext.Provider value={{name, toggleName}}>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
)
|
||||
}
|
||||
function CounterDisplay({value}) {
|
||||
return (<div className={styles.counter_display}>
|
||||
Значение счетчика: <b>{value}</b>
|
||||
</div>)
|
||||
|
||||
function Form() {
|
||||
const {name, toggleName} = useContext(UserContext)
|
||||
const [newName,setNewName] = useState('');
|
||||
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 App() {
|
||||
const [counter,setCounter] = useState(0);
|
||||
function ExitButton() {
|
||||
const {name, toggleName} = useContext(UserContext)
|
||||
|
||||
const increment = () => setCounter(prev => prev + 1);
|
||||
const decrement = () => setCounter(prev => prev - 1);
|
||||
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() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2>Практика по созданию счетчика с колбэками</h2>
|
||||
<CounterButton label="Увеличить" onClick={increment} />
|
||||
<CounterButton label="уменьшить" onClick={decrement} />
|
||||
<CounterDisplay value={counter} />
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<h1>Hello, React!</h1>
|
||||
<UserProvider>
|
||||
<Page />
|
||||
</UserProvider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
.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;
|
||||
}
|
||||
Reference in New Issue
Block a user