diff --git a/src/App.jsx b/src/App.jsx index 7e35cc8..2a6b4ab 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -72,11 +72,37 @@ class MyComponent3 extends React.Component { } } } + +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

Прошло секунд: {this.state.seconds}

+ } +} + function App() { + const [show,setShow] = React.useState(true); + return ( <> -

Hello, React!

- +

Таймер с классом

+ + {show && } ) } diff --git a/src/main.jsx b/src/main.jsx index 950e96a..7e925e8 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client' import App from './App.jsx' createRoot(document.getElementById('root')).render( - - )