Compare commits

..

2 Commits

Author SHA1 Message Date
laktionov-as 12515f925d Сделали таймер 2026-05-28 20:45:57 +03:00
laktionov-as b3662b759f class components 2026-05-28 20:29:26 +03:00
3 changed files with 94 additions and 53 deletions
+89 -31
View File
@@ -1,50 +1,108 @@
import { useState, useReducer } from 'react'
import React from 'react'
function counterReducer(state, action) {
switch (action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
case 'reset':
return 0
case 'set_value':
return action.value
default:
return state
}
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { data: null };
this.handleClick = this.handleClick.bind(this);
}
function Counter() {
const [count, dispatch] = useReducer(counterReducer,0);
render() {
return (
<div>
<span>Счетчик: {count}</span>
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
{this.state.data ? (
<div>Данные: {this.state.data}</div>
) : (
<div>Загрузка...</div>
)}
<button onClick={this.handleClick}>Обновить</button>
</div>
)
}
function App() {
const [tasks, setTasks] = useState([])
const [filter, setFilter] = useState('all')
const [sortBy,setSortBy] = useState('date')
componentDidMount() {
fetch('https://api.example.com/data')
.then(responce => responce.json())
.then(json => this.setState({data: json}));
const addTask = newTask => {
setTasks([...tasks,newTask])
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() {
const [show,setShow] = React.useState(true);
return (
<>
{/* Добавьте тестовый контент для проверки */}
<h1>Hello, React!</h1>
<Counter />
<h1>Таймер с классом</h1>
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
{show && <Timer />}
</>
)
}
-15
View File
@@ -1,15 +0,0 @@
.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;
}
-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>
)