Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 0e043df71e Конец урока 2026-06-11 19:14:13 +03:00
3 changed files with 66 additions and 1279 deletions
+9 -1256
View File
File diff suppressed because it is too large Load Diff
+1 -4
View File
@@ -4,13 +4,12 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"info": "^1.0.0",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
@@ -23,8 +22,6 @@
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"sass": "^1.99.0",
"sass-embedded": "^1.99.0",
"vite": "^6.3.5"
}
}
+56 -19
View File
@@ -1,30 +1,67 @@
import styles from './styles/App.module.scss'
import React, { useState, useCallback } from 'react'
import React, { useState, useReducer, useRef, memo } from 'react'
function CounterButton({label, onClick}) {
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 (
<button onClick={onClick} className={styles.counter_button}>{label}</button>
<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 CounterDisplay({value}) {
return (<div className={styles.counter_display}>
Значение счетчика: <b>{value}</b>
</div>)
}
})
function App() {
const [counter,setCounter] = useState(0);
const [tasks, dispatch] = useReducer(tasksReducer,[])
const [input,setInput] = useState('')
const [error,setError] = useState('')
const inputRef = useRef(null)
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
const addTask = () => {
if (input.trim() === '') {
setError('Пустой текст задачи')
return
}
setError('')
dispatch({
type: 'ADD_TASK',
payload: {id: Date.now(), text: input.trim(), complite: false }
})
setInput('')
inputRef.current.focus()
}
return (
<>
<h2>Практика по созданию счетчика с колбэками</h2>
<CounterButton label="Увеличить" onClick={increment} />
<CounterButton label="уменьшить" onClick={decrement} />
<CounterDisplay value={counter} />
</>
<div style={{padding: 20}}>
<h1>Список дел</h1>
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
<button onClick={addTask}>Добавить</button>
{error && <p style={{color: 'red'}}>{error}</p>}
<TaskList tasks={tasks}
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
</div>
)
}