Compare commits

..

3 Commits

2 changed files with 49 additions and 59 deletions
+49 -57
View File
@@ -1,67 +1,59 @@
import React, { useState, useReducer, useRef, memo } from 'react'
import React from 'react'
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
class RandomUser extends React.Component {
constructor(props) {
super(props)
this.state = { user: {} }
}
componentDidMount() {
fetch('https://randomuser.me/api')
.then(responce => responce.json())
.then(json => this.setState({ user: json.results[0] }));
console.log('смонтирован')
}
componentDidUpdate(prevProps,prevState) {
console.log(this.state);
console.log('Компонент обнвлен')
}
componentWillUnmount() {
console.log('Компонент таймера размонтирован')
}
render() {
return <div>
{this.state.user &&
this.state.user.name &&
(
<>
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
<img src={this.state.user.picture.medium} />
<button onClick={() => {
console.log('Загружаем ... ')
fetch('https://randomuser.me/api')
.then(responce => responce.json())
.then(json => this.setState({ user: json.results[0] }));
}}>Загрузить нового</button>
</>
)
}
</div>
}
}
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
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()
}
const [showClock, setShowClock] = React.useState(true);
return (
<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>
<>
<h1>Домашнее задание: жизненый цикл</h1>
<button onClick={()=> setShowClock(!showClock)}>
{showClock ? 'Скрыть часы' : 'Показать часы' }
</button>
{showClock && <RandomUser />}
</>
)
}
-2
View File
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
)