Compare commits

..

2 Commits

4 changed files with 44 additions and 1281 deletions
+9 -1256
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -10,7 +10,6 @@
"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"
}, },
@@ -23,8 +22,6 @@
"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"
} }
} }
+34 -19
View File
@@ -1,29 +1,44 @@
import styles from './styles/App.module.scss' import React from 'react'
import React, { useState, useCallback } from 'react'
function CounterButton({label, onClick}) { class Clock extends React.Component {
return ( constructor(props) {
<button onClick={onClick} className={styles.counter_button}>{label}</button> super(props)
) this.state = { time: new Date().toLocaleTimeString() }
} }
function CounterDisplay({value}) {
return (<div className={styles.counter_display}> componentDidMount() {
Значение счетчика: <b>{value}</b> this.timerId = setInterval(()=>{
</div>) this.setState({ time: new Date().toLocaleTimeString() })
}, 1000);
console.log('Таймер смонтирован')
}
componentDidUpdate(prevProps,prevState) {
console.log('Компонент обнвлен')
}
componentWillUnmount() {
clearInterval(this.timerId)
console.log('Компонент таймера размонтирован')
}
render() {
return <div>
<h2>Текущее время: {this.state.time}</h2>
</div>
}
} }
function App() { function App() {
const [counter,setCounter] = useState(0); const [showClock, setShowClock] = React.useState(true);
const increment = () => setCounter(prev => prev + 1);
const decrement = () => setCounter(prev => prev - 1);
return ( return (
<> <>
<h2>Практика по созданию счетчика с колбэками</h2> <h1>Жизненный цикл компонента часов</h1>
<CounterButton label="Увеличить" onClick={increment} /> <button onClick={()=> setShowClock(!showClock)}>
<CounterButton label="уменьшить" onClick={decrement} /> {showClock ? 'Скрыть часы' : 'Показать часы' }
<CounterDisplay value={counter} /> </button>
{showClock && <Clock />}
</> </>
) )
} }
-2
View File
@@ -4,7 +4,5 @@ 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>
) )