Compare commits
3 Commits
lesson-4.1
...
lesson-7.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
+70
-2
@@ -1,10 +1,78 @@
|
||||
// удаляем все, оставляя лишь пустую функцию App ниже
|
||||
import { useState, createContext, useContext } from 'react'
|
||||
import styles from './App.module.scss'
|
||||
|
||||
function App() {
|
||||
const UserContext = createContext('')
|
||||
|
||||
function UserProvider({ children }) {
|
||||
const [name,setName] = useState('');
|
||||
|
||||
const toggleName = (newName) => {
|
||||
setName(newName);
|
||||
}
|
||||
|
||||
return (
|
||||
<UserContext.Provider value={{name, toggleName}}>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
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 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() {
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<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