Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+55
-97
@@ -1,109 +1,67 @@
|
|||||||
import React from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
|
|
||||||
class MyComponent extends React.Component {
|
function tasksReducer(state, action) {
|
||||||
constructor(props) {
|
switch(action.type) {
|
||||||
super(props);
|
case 'ADD_TASK':
|
||||||
this.state = { data: null };
|
return [...state, action.payload]
|
||||||
this.handleClick = this.handleClick.bind(this);
|
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
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
|
console.log("Rendred")
|
||||||
return (
|
return (
|
||||||
<div>
|
<ul>
|
||||||
{this.state.data ? (
|
{tasks.map(({id, text, complite}) => (
|
||||||
<div>Данные: {this.state.data}</div>
|
<li key={id}
|
||||||
) : (
|
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||||
<div>Загрузка...</div>
|
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||||
)}
|
{text}
|
||||||
<button onClick={this.handleClick}>Обновить</button>
|
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||||
</div>
|
Удалить
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
fetch('https://api.example.com/data')
|
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({data: json}));
|
|
||||||
|
|
||||||
this.timerId = setInterval(()=> console.log('Tick'), 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmout() {
|
|
||||||
clearInterval(this.timerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleClick() {
|
|
||||||
this.setState({data: 'Данные'})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MyComponent2 extends React.Component {
|
|
||||||
controller = new AbortController()
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({data: json}))
|
|
||||||
.catch(error => {
|
|
||||||
if(error.name == 'AbortError') {
|
|
||||||
console.log('Aborted');
|
|
||||||
} else {
|
|
||||||
console.log('Other error',error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount() {
|
|
||||||
this.controller.abort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
class MyComponent3 extends React.Component {
|
|
||||||
componentDidMount() {
|
|
||||||
this.loadData();
|
|
||||||
}
|
|
||||||
|
|
||||||
async loadData() {
|
|
||||||
try {
|
|
||||||
const response = await fetch('https://api.example.com/data');
|
|
||||||
const data = await response.json();
|
|
||||||
this.setSate({ data });
|
|
||||||
} catch {
|
|
||||||
console.log('error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Timer extends React.Component {
|
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
this.state = { seconds: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
console.log('Timer mounted');
|
|
||||||
this.interval = setInterval(()=>{
|
|
||||||
this.setState(prevState => ({seconds: prevState.seconds + 1}))
|
|
||||||
},1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount() {
|
|
||||||
console.log('Timer unmount');
|
|
||||||
clearInterval(this.interval);
|
|
||||||
}
|
|
||||||
render() {
|
|
||||||
return <h2>Прошло секунд: {this.state.seconds}</h2>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [show,setShow] = 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={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
{show && <Timer />}
|
<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>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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