Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aeb40b20 |
+33
-47
@@ -1,58 +1,44 @@
|
|||||||
import React from 'react'
|
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
||||||
|
import PostAddForm from './components/PostAddForm'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
/*
|
||||||
constructor(props) {
|
const mutation = useMutation({
|
||||||
super(props)
|
mutationFn: newUser => {
|
||||||
this.state = { user: {} }
|
fetch('https://jsonplaceholder.typicode.com/users', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {'Content-type':'application/json'},
|
||||||
|
body: JSON.stringify(newUser),
|
||||||
|
}).then(res => res.json())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
componentDidMount() {
|
*/
|
||||||
fetch('https://randomuser.me/api')
|
|
||||||
.then(responce => responce.json())
|
const createQueryClient = () => {
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
new QueryClient({
|
||||||
console.log('смонтирован')
|
defaultOptions: {
|
||||||
}
|
queries: {
|
||||||
|
staleTime: 5 * 60*1000,
|
||||||
componentDidUpdate(prevProps,prevState) {
|
retry: 1,
|
||||||
console.log(this.state);
|
gcTime: 10*60*1000,
|
||||||
console.log('Компонент обнвлен')
|
refetchOnWindowFocus: false,
|
||||||
}
|
},
|
||||||
|
mutations: {
|
||||||
componentWillUnmount() {
|
retry:0,
|
||||||
console.log('Компонент таймера размонтирован')
|
onError: error => {console.error('Mutation error:',error.message)}
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<h1>Hello, React!</h1>
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<QueryClientProvider client={createQueryClient}>
|
||||||
</button>
|
<PostAddForm />
|
||||||
{showClock && <RandomUser />}
|
</QueryClientProvider>
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user