Compare commits

..

3 Commits

2 changed files with 47 additions and 35 deletions
+47 -33
View File
@@ -1,44 +1,58 @@
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query' import React from 'react'
import PostAddForm from './components/PostAddForm'
/* class RandomUser extends React.Component {
const mutation = useMutation({ constructor(props) {
mutationFn: newUser => { super(props)
fetch('https://jsonplaceholder.typicode.com/users', { this.state = { user: {} }
method: 'GET',
headers: {'Content-type':'application/json'},
body: JSON.stringify(newUser),
}).then(res => res.json())
} }
})
*/ componentDidMount() {
fetch('https://randomuser.me/api')
const createQueryClient = () => { .then(responce => responce.json())
new QueryClient({ .then(json => this.setState({ user: json.results[0] }));
defaultOptions: { console.log('смонтирован')
queries: { }
staleTime: 5 * 60*1000,
retry: 1, componentDidUpdate(prevProps,prevState) {
gcTime: 10*60*1000, console.log(this.state);
refetchOnWindowFocus: false, console.log('Компонент обнвлен')
}, }
mutations: {
retry:0, componentWillUnmount() {
onError: error => {console.error('Mutation error:',error.message)} console.log('Компонент таймера размонтирован')
}
render() {
return <div>
{this.state.user &&
this.state.user.name &&
(
<>
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
<img src={this.state.user.picture.medium} />
<button onClick={() => {
console.log('Загружаем ... ')
fetch('https://randomuser.me/api')
.then(responce => responce.json())
.then(json => this.setState({ user: json.results[0] }));
}}>Загрузить нового</button>
</>
)
} }
} </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 && <RandomUser />}
</> </>
) )
} }
-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>
) )