Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 0e043df71e Конец урока 2026-06-11 19:14:13 +03:00
5 changed files with 67 additions and 1302 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", "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": {
"info": "^1.0.0",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0"
}, },
@@ -23,8 +22,6 @@
"eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19", "eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0", "globals": "^16.0.0",
"sass": "^1.99.0",
"sass-embedded": "^1.99.0",
"vite": "^6.3.5" "vite": "^6.3.5"
} }
} }
+56 -19
View File
@@ -1,30 +1,67 @@
import styles from './styles/App.module.scss' import React, { useState, useReducer, useRef, memo } from 'react'
import React, { useState, useCallback } 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 ( 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() { 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 addTask = () => {
const decrement = () => setCounter(prev => prev - 1); if (input.trim() === '') {
setError('Пустой текст задачи')
return
}
setError('')
dispatch({
type: 'ADD_TASK',
payload: {id: Date.now(), text: input.trim(), complite: false }
})
setInput('')
inputRef.current.focus()
}
return ( return (
<> <div style={{padding: 20}}>
<h2>Практика по созданию счетчика с колбэками</h2> <h1>Список дел</h1>
<CounterButton label="Увеличить" onClick={increment} /> <input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
<CounterButton label="уменьшить" onClick={decrement} /> <button onClick={addTask}>Добавить</button>
<CounterDisplay value={counter} /> {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>
) )
} }
+1 -1
View File
@@ -1,6 +1,6 @@
import { StrictMode } from 'react' import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client' import { createRoot } from 'react-dom/client'
// удаляем import './index.css'
import App from './App.jsx' import App from './App.jsx'
createRoot(document.getElementById('root')).render( createRoot(document.getElementById('root')).render(
-22
View File
@@ -1,22 +0,0 @@
.container {
display:flex;
flex-direction: row;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
p {
padding: 5px 10px;
}
}
.counter {
&_display{
border: 1px solid black;
border-width: 3px;
border-radius: 10px;
padding: 10px;
width: 300px;
}
&_button{
padding: 10px;
}
}