Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b70fa47bbc | |||
| bd35029fc1 | |||
| 92dc5df96b | |||
| 3d34262203 | |||
| 1bc2e883a1 | |||
| 8a8a17f44d | |||
| 8f46470a26 |
Generated
+1256
-9
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"info": "^1.0.0",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -22,6 +23,8 @@
|
|||||||
"eslint-plugin-react-hooks": "^5.2.0",
|
"eslint-plugin-react-hooks": "^5.2.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.19",
|
"eslint-plugin-react-refresh": "^0.4.19",
|
||||||
"globals": "^16.0.0",
|
"globals": "^16.0.0",
|
||||||
|
"sass": "^1.99.0",
|
||||||
|
"sass-embedded": "^1.99.0",
|
||||||
"vite": "^6.3.5"
|
"vite": "^6.3.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-95
@@ -1,108 +1,29 @@
|
|||||||
import React from 'react'
|
import styles from './styles/App.module.scss'
|
||||||
|
import React, { useState, useCallback } from 'react'
|
||||||
|
|
||||||
class MyComponent extends React.Component {
|
function CounterButton({label, onClick}) {
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
this.state = { data: null };
|
|
||||||
this.handleClick = this.handleClick.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<button onClick={onClick} className={styles.counter_button}>{label}</button>
|
||||||
{this.state.data ? (
|
|
||||||
<div>Данные: {this.state.data}</div>
|
|
||||||
) : (
|
|
||||||
<div>Загрузка...</div>
|
|
||||||
)}
|
|
||||||
<button onClick={this.handleClick}>Обновить</button>
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
function CounterDisplay({value}) {
|
||||||
componentDidMount() {
|
return (<div className={styles.counter_display}>
|
||||||
fetch('https://api.example.com/data')
|
Значение счетчика: <b>{value}</b>
|
||||||
.then(responce => responce.json())
|
</div>)
|
||||||
.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 [counter,setCounter] = useState(0);
|
||||||
|
|
||||||
|
const increment = () => setCounter(prev => prev + 1);
|
||||||
|
const decrement = () => setCounter(prev => prev - 1);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Таймер с классом</h1>
|
<h2>Практика по созданию счетчика с колбэками</h2>
|
||||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
<CounterButton label="Увеличить" onClick={increment} />
|
||||||
{show && <Timer />}
|
<CounterButton label="уменьшить" onClick={decrement} />
|
||||||
|
<CounterDisplay value={counter} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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