Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de33066cbe |
Generated
+11
@@ -8,6 +8,7 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
@@ -2054,6 +2055,16 @@
|
|||||||
"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",
|
||||||
|
|||||||
+2
-1
@@ -4,12 +4,13 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
+39
-56
@@ -1,67 +1,50 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
|
import { produce } from 'immer'
|
||||||
function tasksReducer(state, action) {
|
|
||||||
switch(action.type) {
|
|
||||||
case 'ADD_TASK':
|
|
||||||
return [...state, action.payload]
|
|
||||||
case 'TOGGLE_TASK':
|
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
|
||||||
case 'REMOVE_TASK':
|
|
||||||
return state.filter(task => task.id !== action.payload)
|
|
||||||
default:
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
|
||||||
console.log("Rendred")
|
|
||||||
return (
|
|
||||||
<ul>
|
|
||||||
{tasks.map(({id, text, complite}) => (
|
|
||||||
<li key={id}
|
|
||||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
|
||||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
|
||||||
{text}
|
|
||||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
|
||||||
Удалить
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
const [tasks,setTasks] = useState([])
|
||||||
const [input,setInput] = useState('')
|
const [input,setInput] = useState('')
|
||||||
const [error,setError] = useState('')
|
|
||||||
const inputRef = useRef(null)
|
|
||||||
|
|
||||||
const addTask = () => {
|
const addTask = () => {
|
||||||
if (input.trim() === '') {
|
if(!input.trim()) return
|
||||||
setError('Пустой текст задачи')
|
|
||||||
return
|
setTasks(currentTasks => produce(currentTasks, draft => {
|
||||||
}
|
draft.push({
|
||||||
setError('')
|
id: Date.now(),
|
||||||
dispatch({
|
text: input.trim(),
|
||||||
type: 'ADD_TASK',
|
done: false
|
||||||
payload: {id: Date.now(), text: input.trim(), complite: false }
|
})
|
||||||
})
|
}))
|
||||||
setInput('')
|
setInput('')
|
||||||
inputRef.current.focus()
|
|
||||||
}
|
}
|
||||||
|
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 (
|
||||||
<div style={{padding: 20}}>
|
<>
|
||||||
<h1>Список дел</h1>
|
<h1>Практика по созданию списка задач</h1>
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
<input value={input} onChange={e=>setInput(e.target.value)} />
|
||||||
<button onClick={addTask}>Добавить</button>
|
<button onClick={addTask}>Добавить</button>
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
<ul>{tasks.map(({id,text,done}) => (
|
||||||
|
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
||||||
<TaskList tasks={tasks}
|
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
{text}
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
<button onClick={() => removeTask(id)}>Удалить</button>
|
||||||
</div>
|
</li>
|
||||||
|
))}</ul>
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export function MyComponent() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Hello</h1>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
export default MyComponent
|
||||||
@@ -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