Compare commits

..

2 Commits

2 changed files with 34 additions and 36 deletions
+33 -33
View File
@@ -1,44 +1,44 @@
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query' import React from 'react'
import PostAddForm from './components/PostAddForm'
/* class Clock extends React.Component {
const mutation = useMutation({ constructor(props) {
mutationFn: newUser => { super(props)
fetch('https://jsonplaceholder.typicode.com/users', { this.state = { time: new Date().toLocaleTimeString() }
method: 'GET',
headers: {'Content-type':'application/json'},
body: JSON.stringify(newUser),
}).then(res => res.json())
} }
})
*/
const createQueryClient = () => { componentDidMount() {
new QueryClient({ this.timerId = setInterval(()=>{
defaultOptions: { this.setState({ time: new Date().toLocaleTimeString() })
queries: { }, 1000);
staleTime: 5 * 60*1000, console.log('Таймер смонтирован')
retry: 1, }
gcTime: 10*60*1000,
refetchOnWindowFocus: false, componentDidUpdate(prevProps,prevState) {
}, console.log('Компонент обнвлен')
mutations: { }
retry:0,
onError: error => {console.error('Mutation error:',error.message)} componentWillUnmount() {
} 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>
<h1>Hello, React!</h1> <button onClick={()=> setShowClock(!showClock)}>
<QueryClientProvider client={createQueryClient}> {showClock ? 'Скрыть часы' : 'Показать часы' }
<PostAddForm /> </button>
</QueryClientProvider> {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>
) )