Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+55
-39
@@ -1,52 +1,68 @@
|
|||||||
import { useState, useReducer } from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
|
|
||||||
function counterReducer(state, action) {
|
function tasksReducer(state, action) {
|
||||||
switch (action.type) {
|
switch(action.type) {
|
||||||
case 'increment':
|
case 'ADD_TASK':
|
||||||
return state + 1
|
return [...state, action.payload]
|
||||||
case 'decrement':
|
case 'TOGGLE_TASK':
|
||||||
return state - 1
|
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||||
case 'reset':
|
case 'REMOVE_TASK':
|
||||||
return 0
|
return state.filter(task => task.id !== action.payload)
|
||||||
case 'set_value':
|
|
||||||
return action.value
|
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Counter() {
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
const [count, dispatch] = useReducer(counterReducer,0);
|
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() {
|
||||||
|
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
||||||
|
const [input,setInput] = useState('')
|
||||||
|
const [error,setError] = useState('')
|
||||||
|
const inputRef = useRef(null)
|
||||||
|
|
||||||
|
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 (
|
return (
|
||||||
<div>
|
<div style={{padding: 20}}>
|
||||||
<span>Счетчик: {count}</span>
|
<h1>Список дел</h1>
|
||||||
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
<button onClick={addTask}>Добавить</button>
|
||||||
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
|
||||||
|
<TaskList tasks={tasks}
|
||||||
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
|
||||||
const [tasks, setTasks] = useState([])
|
|
||||||
const [filter, setFilter] = useState('all')
|
|
||||||
const [sortBy,setSortBy] = useState('date')
|
|
||||||
|
|
||||||
const addTask = newTask => {
|
|
||||||
setTasks([...tasks,newTask])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
|
||||||
<h1>Hello, React!</h1>
|
|
||||||
<Counter />
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
.container {
|
|
||||||
/* display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
flex-wrap: nowrap;*/
|
|
||||||
height: 500px;
|
|
||||||
overflow: auto;
|
|
||||||
border: 1px solid black;
|
|
||||||
}
|
|
||||||
.item {
|
|
||||||
font-size: 16pt;
|
|
||||||
border: 1px dotted gray;
|
|
||||||
/*margin: 5px 10px;
|
|
||||||
padding: 10px;*/
|
|
||||||
border-radius: 5px;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user