Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23db798577 | |||
| c99a045400 |
+36
-94
@@ -1,108 +1,50 @@
|
|||||||
import React from 'react'
|
import { useState, useReducer } from 'react'
|
||||||
|
|
||||||
class MyComponent extends React.Component {
|
function counterReducer(state, action) {
|
||||||
constructor(props) {
|
switch (action.type) {
|
||||||
super(props);
|
case 'increment':
|
||||||
this.state = { data: null };
|
return state + 1
|
||||||
this.handleClick = this.handleClick.bind(this);
|
case 'decrement':
|
||||||
}
|
return state - 1
|
||||||
|
case 'reset':
|
||||||
render() {
|
return 0
|
||||||
return (
|
case 'set_value':
|
||||||
<div>
|
return action.value
|
||||||
{this.state.data ? (
|
default:
|
||||||
<div>Данные: {this.state.data}</div>
|
return state
|
||||||
) : (
|
|
||||||
<div>Загрузка...</div>
|
|
||||||
)}
|
|
||||||
<button onClick={this.handleClick}>Обновить</button>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
function Counter() {
|
||||||
controller = new AbortController()
|
const [count, dispatch] = useReducer(counterReducer,0);
|
||||||
|
|
||||||
componentDidMount() {
|
return (
|
||||||
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
<div>
|
||||||
.then(responce => responce.json())
|
<span>Счетчик: {count}</span>
|
||||||
.then(json => this.setState({data: json}))
|
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||||
.catch(error => {
|
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||||
if(error.name == 'AbortError') {
|
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||||
console.log('Aborted');
|
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||||
} else {
|
</div>
|
||||||
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, setTasks] = useState([])
|
||||||
|
const [filter, setFilter] = useState('all')
|
||||||
|
const [sortBy,setSortBy] = useState('date')
|
||||||
|
|
||||||
|
const addTask = newTask => {
|
||||||
|
setTasks([...tasks,newTask])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Таймер с классом</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
<h1>Hello, React!</h1>
|
||||||
{show && <Timer />}
|
<Counter />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
.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;
|
||||||
|
}
|
||||||
@@ -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