Я уже не помню что там было
This commit is contained in:
+60
-32
@@ -1,50 +1,78 @@
|
||||
import { useState, useReducer } from 'react'
|
||||
import { useState, createContext, useContext } from 'react'
|
||||
import styles from './App.module.scss'
|
||||
|
||||
function counterReducer(state, action) {
|
||||
switch (action.type) {
|
||||
case 'increment':
|
||||
return state + 1
|
||||
case 'decrement':
|
||||
return state - 1
|
||||
case 'reset':
|
||||
return 0
|
||||
case 'set_value':
|
||||
return action.value
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
const UserContext = createContext('')
|
||||
|
||||
function Counter() {
|
||||
const [count, dispatch] = useReducer(counterReducer,0);
|
||||
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>
|
||||
<span>Счетчик: {count}</span>
|
||||
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||
<Toolbar />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
const [tasks, setTasks] = useState([])
|
||||
const [filter, setFilter] = useState('all')
|
||||
const [sortBy,setSortBy] = useState('date')
|
||||
|
||||
const addTask = newTask => {
|
||||
setTasks([...tasks,newTask])
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<h1>Hello, React!</h1>
|
||||
<Counter />
|
||||
<UserProvider>
|
||||
<Page />
|
||||
</UserProvider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
+2
-4
@@ -1,10 +1,8 @@
|
||||
.container {
|
||||
/* display: flex;
|
||||
flex-direction: column;
|
||||
flex-wrap: nowrap;*/
|
||||
height: 500px;
|
||||
overflow: auto;
|
||||
border: 1px solid black;
|
||||
border-radius: 10px;
|
||||
background-color: lightblue;
|
||||
}
|
||||
.item {
|
||||
font-size: 16pt;
|
||||
|
||||
Reference in New Issue
Block a user