Заготовка компонента с жизнеными циклами

This commit is contained in:
2026-06-02 19:42:31 +03:00
parent ec325b51bc
commit c137c644e3
2 changed files with 31 additions and 4 deletions
+31 -2
View File
@@ -1,10 +1,39 @@
// удаляем все, оставляя лишь пустую функцию App ниже
import React from 'react'
class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = { count: 0 }
}
componentDidMount() {
console.log('Компонент смонтирован')
}
componentDidUpdate(prevProps,prevState) {
console.log('Компонент обнвлен')
if(prevState.count !== this.state.count) {
console.log('Состояние изменилось')
}
}
componentWillUnmount() {
console.log('Компонент будет размонтирован')
}
render() {
return <div>
Привет мир! {this.props.name} {this.state.count}
<button onClick={()=> this.setState({ count: this.state.count })}>Жми!</button>
</div>
}
}
function App() {
return (
<>
{/* Добавьте тестовый контент для проверки */}
<h1>Hello, React!</h1>
<MyComponent name="Alex" />
</>
)
}
-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>
)