Compare commits

..

2 Commits

2 changed files with 34 additions and 36 deletions
+34 -34
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), componentDidMount() {
}).then(res => res.json()) this.timerId = setInterval(()=>{
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>
} }
})
*/
const createQueryClient = () => {
new QueryClient({
defaultOptions: {
queries: {
staleTime: 5 * 60*1000,
retry: 1,
gcTime: 10*60*1000,
refetchOnWindowFocus: false,
},
mutations: {
retry:0,
onError: error => {console.error('Mutation error:',error.message)}
}
}
})
} }
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>
) )