Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
Generated
-11
@@ -8,7 +8,6 @@
|
|||||||
"name": "store-client",
|
"name": "store-client",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -2055,16 +2054,6 @@
|
|||||||
"node": ">= 4"
|
"node": ">= 4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/immer": {
|
|
||||||
"version": "11.1.8",
|
|
||||||
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
|
|
||||||
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
|
|
||||||
"license": "MIT",
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/immer"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/import-fresh": {
|
"node_modules/import-fresh": {
|
||||||
"version": "3.3.1",
|
"version": "3.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+70
-41
@@ -1,49 +1,78 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import { useState, createContext, useContext } from 'react'
|
||||||
import { produce } from 'immer'
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
|
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() {
|
function App() {
|
||||||
const [tasks,setTasks] = useState([])
|
|
||||||
const [input,setInput] = useState('')
|
|
||||||
|
|
||||||
const addTask = () => {
|
|
||||||
if(!input.trim()) return
|
|
||||||
|
|
||||||
setTasks(currentTasks => produce(currentTasks, draft => {
|
|
||||||
draft.push({
|
|
||||||
id: Date.now(),
|
|
||||||
text: input.trim(),
|
|
||||||
done: false
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
setInput('')
|
|
||||||
}
|
|
||||||
const toggleTask = id => {
|
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
|
||||||
const task = draft.find(t => t.id === id)
|
|
||||||
if(task) {
|
|
||||||
task.done = !task.done
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
const removeTask = id => {
|
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
|
||||||
const index = draft.findIndex(t => t.id === id)
|
|
||||||
if (index !== -1)
|
|
||||||
draft.splice(index,1)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Практика по созданию списка задач</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={addTask}>Добавить</button>
|
<UserProvider>
|
||||||
<ul>{tasks.map(({id,text,done}) => (
|
<Page />
|
||||||
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
</UserProvider>
|
||||||
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
|
||||||
{text}
|
|
||||||
<button onClick={() => removeTask(id)}>Удалить</button>
|
|
||||||
</li>
|
|
||||||
))}</ul>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
export function MyComponent() {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<h1>Hello</h1>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export default MyComponent
|
|
||||||
@@ -4,5 +4,7 @@ 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