Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23db798577 | |||
| c99a045400 |
+34
-50
@@ -1,67 +1,51 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import { useState, useReducer } from 'react'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
function counterReducer(state, action) {
|
||||||
switch(action.type) {
|
switch (action.type) {
|
||||||
case 'ADD_TASK':
|
case 'increment':
|
||||||
return [...state, action.payload]
|
return state + 1
|
||||||
case 'TOGGLE_TASK':
|
case 'decrement':
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
return state - 1
|
||||||
case 'REMOVE_TASK':
|
case 'reset':
|
||||||
return state.filter(task => task.id !== action.payload)
|
return 0
|
||||||
|
case 'set_value':
|
||||||
|
return action.value
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
function Counter() {
|
||||||
console.log("Rendred")
|
const [count, dispatch] = useReducer(counterReducer,0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul>
|
<div>
|
||||||
{tasks.map(({id, text, complite}) => (
|
<span>Счетчик: {count}</span>
|
||||||
<li key={id}
|
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||||
{text}
|
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
</div>
|
||||||
Удалить
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
const [tasks, setTasks] = useState([])
|
||||||
const [input,setInput] = useState('')
|
const [filter, setFilter] = useState('all')
|
||||||
const [error,setError] = useState('')
|
const [sortBy,setSortBy] = useState('date')
|
||||||
const inputRef = useRef(null)
|
|
||||||
|
|
||||||
const addTask = () => {
|
const addTask = newTask => {
|
||||||
if (input.trim() === '') {
|
setTasks([...tasks,newTask])
|
||||||
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}}>
|
<>
|
||||||
<h1>Список дел</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={addTask}>Добавить</button>
|
<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>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
.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