Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ee1bf2eee | |||
| c137c644e3 |
+35
-57
@@ -1,67 +1,45 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
class Clock extends React.Component {
|
||||||
switch(action.type) {
|
constructor(props) {
|
||||||
case 'ADD_TASK':
|
super(props)
|
||||||
return [...state, action.payload]
|
this.state = { time: new Date().toLocaleTimeString() }
|
||||||
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}) {
|
componentDidMount() {
|
||||||
console.log("Rendred")
|
this.timerId = setInterval(()=>{
|
||||||
return (
|
this.setState({ time: new Date().toLocaleTimeString() })
|
||||||
<ul>
|
}, 1000);
|
||||||
{tasks.map(({id, text, complite}) => (
|
console.log('Таймер смонтирован')
|
||||||
<li key={id}
|
}
|
||||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
|
||||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
componentDidUpdate(prevProps,prevState) {
|
||||||
{text}
|
console.log('Компонент обнвлен')
|
||||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
}
|
||||||
Удалить
|
|
||||||
</button>
|
componentWillUnmount() {
|
||||||
</li>
|
clearInterval(this.timerId)
|
||||||
))}
|
console.log('Компонент таймера размонтирован')
|
||||||
</ul>
|
}
|
||||||
)
|
|
||||||
})
|
render() {
|
||||||
|
return <div>
|
||||||
|
<h2>Текущее время: {this.state.time}</h2>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
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 style={{padding: 20}}>
|
<>
|
||||||
<h1>Список дел</h1>
|
<h1>Жизненный цикл компонента часов</h1>
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
<button onClick={addTask}>Добавить</button>
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
</button>
|
||||||
|
{showClock && <Clock />}
|
||||||
<TaskList tasks={tasks}
|
</>
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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