Compare commits

..

1 Commits

Author SHA1 Message Date
laktionov-as 43aeb40b20 i dont know 2026-06-18 18:53:17 +03:00
2 changed files with 36 additions and 34 deletions
+33 -33
View File
@@ -1,44 +1,44 @@
import React from 'react' import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
import PostAddForm from './components/PostAddForm'
class Clock extends React.Component { /*
constructor(props) { const mutation = useMutation({
super(props) mutationFn: newUser => {
this.state = { time: new Date().toLocaleTimeString() } fetch('https://jsonplaceholder.typicode.com/users', {
method: 'GET',
headers: {'Content-type':'application/json'},
body: JSON.stringify(newUser),
}).then(res => res.json())
} }
})
*/
componentDidMount() { const createQueryClient = () => {
this.timerId = setInterval(()=>{ new QueryClient({
this.setState({ time: new Date().toLocaleTimeString() }) defaultOptions: {
}, 1000); queries: {
console.log('Таймер смонтирован') staleTime: 5 * 60*1000,
} retry: 1,
gcTime: 10*60*1000,
componentDidUpdate(prevProps,prevState) { refetchOnWindowFocus: false,
console.log('Компонент обнвлен') },
} mutations: {
retry:0,
componentWillUnmount() { onError: error => {console.error('Mutation error:',error.message)}
clearInterval(this.timerId) }
console.log('Компонент таймера размонтирован') }
} })
render() {
return <div>
<h2>Текущее время: {this.state.time}</h2>
</div>
}
} }
function App() { function App() {
const [showClock, setShowClock] = React.useState(true);
return ( return (
<> <>
<h1>Жизненный цикл компонента часов</h1> {/* Добавьте тестовый контент для проверки */}
<button onClick={()=> setShowClock(!showClock)}> <h1>Hello, React!</h1>
{showClock ? 'Скрыть часы' : 'Показать часы' } <QueryClientProvider client={createQueryClient}>
</button> <PostAddForm />
{showClock && <Clock />} </QueryClientProvider>
</> </>
) )
} }
+2
View File
@@ -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>
) )