Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8cc72afd7b | |||
| a520406547 | |||
| 38f7195ed4 |
+31
-64
@@ -1,78 +1,45 @@
|
|||||||
import { useState, createContext, useContext } from 'react'
|
import React, { useMemo, useCallback, useState } from 'react'
|
||||||
import styles from './App.module.scss'
|
|
||||||
|
|
||||||
const UserContext = createContext('')
|
const TodoItem = React.memo(({ item, onToggle, onDelete }) => {
|
||||||
|
console.log(`rendred ${item.id}`);
|
||||||
|
return <li>
|
||||||
|
{item.name}
|
||||||
|
<button onClick={() => onToggle(item.id)}>Done</button>
|
||||||
|
<button onClick={() => onDelete(item.id)}>remove</button>
|
||||||
|
</li>
|
||||||
|
})
|
||||||
|
|
||||||
function UserProvider({ children }) {
|
function ItemList({ items }) {
|
||||||
const [name,setName] = useState('');
|
|
||||||
|
const handleToggle = useCallback(id => {
|
||||||
const toggleName = (newName) => {
|
items.done = true;
|
||||||
setName(newName);
|
console.log(`Item ${id} done`)
|
||||||
}
|
console.log(id)
|
||||||
|
},[]);
|
||||||
|
|
||||||
|
const handleDelete = useCallback(id => {
|
||||||
|
items.pop(id);
|
||||||
|
},[]);
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider value={{name, toggleName}}>
|
<>
|
||||||
{children}
|
<h3>List items</h3>
|
||||||
</UserContext.Provider>
|
{items.map(item => (
|
||||||
|
<TodoItem key={item.id} item={item}
|
||||||
|
onToggle={handleToggle} onDelete={handleDelete} />
|
||||||
|
))}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function Form() {
|
function App() {
|
||||||
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<UserProvider>
|
<ItemList items={[
|
||||||
<Page />
|
{id:1,name:'Первый',done:false},
|
||||||
</UserProvider>
|
{id:2,name:'Второй',done:false}
|
||||||
|
]} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
<StrictMode>
|
|
||||||
<App />
|
<App />
|
||||||
</StrictMode>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user