Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+56
-48
@@ -1,59 +1,67 @@
|
|||||||
import React from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
function tasksReducer(state, action) {
|
||||||
constructor(props) {
|
switch(action.type) {
|
||||||
super(props)
|
case 'ADD_TASK':
|
||||||
this.state = { user: {} }
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
fetch('https://randomuser.me/api')
|
console.log("Rendred")
|
||||||
.then(responce => responce.json())
|
return (
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
<ul>
|
||||||
console.log('смонтирован')
|
{tasks.map(({id, text, complite}) => (
|
||||||
}
|
<li key={id}
|
||||||
|
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||||
componentDidUpdate(prevProps,prevState) {
|
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||||
console.log(this.state);
|
{text}
|
||||||
console.log('Компонент обнвлен')
|
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||||
}
|
Удалить
|
||||||
|
</button>
|
||||||
componentWillUnmount() {
|
</li>
|
||||||
console.log('Компонент таймера размонтирован')
|
))}
|
||||||
}
|
</ul>
|
||||||
|
|
||||||
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>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [showClock, setShowClock] = React.useState(true);
|
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 style={{padding: 20}}>
|
||||||
<h1>Домашнее задание: жизненый цикл</h1>
|
<h1>Список дел</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<button onClick={addTask}>Добавить</button>
|
||||||
</button>
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
{showClock && <RandomUser />}
|
|
||||||
</>
|
<TaskList tasks={tasks}
|
||||||
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,7 @@ 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