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 35 additions and 47 deletions
+32 -46
View File
@@ -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() { const createQueryClient = () => {
fetch('https://randomuser.me/api') new QueryClient({
.then(responce => responce.json()) defaultOptions: {
.then(json => this.setState({ user: json.results[0] })); queries: {
console.log('смонтирован') staleTime: 5 * 60*1000,
} retry: 1,
gcTime: 10*60*1000,
componentDidUpdate(prevProps,prevState) { refetchOnWindowFocus: false,
console.log(this.state); },
console.log('Компонент обнвлен') mutations: {
} retry:0,
onError: error => {console.error('Mutation error:',error.message)}
componentWillUnmount() {
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> {/* Добавьте тестовый контент для проверки */}
<button onClick={()=> setShowClock(!showClock)}> <h1>Hello, React!</h1>
{showClock ? 'Скрыть часы' : 'Показать часы' } <QueryClientProvider client={createQueryClient}>
</button> <PostAddForm />
{showClock && <RandomUser />} </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>
) )