Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12515f925d | |||
| b3662b759f |
+95
-31
@@ -1,44 +1,108 @@
|
|||||||
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
import React from 'react'
|
||||||
import PostAddForm from './components/PostAddForm'
|
|
||||||
|
|
||||||
/*
|
class MyComponent extends React.Component {
|
||||||
const mutation = useMutation({
|
constructor(props) {
|
||||||
mutationFn: newUser => {
|
super(props);
|
||||||
fetch('https://jsonplaceholder.typicode.com/users', {
|
this.state = { data: null };
|
||||||
method: 'GET',
|
this.handleClick = this.handleClick.bind(this);
|
||||||
headers: {'Content-type':'application/json'},
|
|
||||||
body: JSON.stringify(newUser),
|
|
||||||
}).then(res => res.json())
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
*/
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{this.state.data ? (
|
||||||
|
<div>Данные: {this.state.data}</div>
|
||||||
|
) : (
|
||||||
|
<div>Загрузка...</div>
|
||||||
|
)}
|
||||||
|
<button onClick={this.handleClick}>Обновить</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
fetch('https://api.example.com/data')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({data: json}));
|
||||||
|
|
||||||
|
this.timerId = setInterval(()=> console.log('Tick'), 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmout() {
|
||||||
|
clearInterval(this.timerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleClick() {
|
||||||
|
this.setState({data: 'Данные'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const createQueryClient = () => {
|
class MyComponent2 extends React.Component {
|
||||||
new QueryClient({
|
controller = new AbortController()
|
||||||
defaultOptions: {
|
|
||||||
queries: {
|
componentDidMount() {
|
||||||
staleTime: 5 * 60*1000,
|
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
||||||
retry: 1,
|
.then(responce => responce.json())
|
||||||
gcTime: 10*60*1000,
|
.then(json => this.setState({data: json}))
|
||||||
refetchOnWindowFocus: false,
|
.catch(error => {
|
||||||
},
|
if(error.name == 'AbortError') {
|
||||||
mutations: {
|
console.log('Aborted');
|
||||||
retry:0,
|
} else {
|
||||||
onError: error => {console.error('Mutation error:',error.message)}
|
console.log('Other error',error);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
this.controller.abort()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class MyComponent3 extends React.Component {
|
||||||
|
componentDidMount() {
|
||||||
|
this.loadData();
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadData() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('https://api.example.com/data');
|
||||||
|
const data = await response.json();
|
||||||
|
this.setSate({ data });
|
||||||
|
} catch {
|
||||||
|
console.log('error');
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Timer extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { seconds: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
console.log('Timer mounted');
|
||||||
|
this.interval = setInterval(()=>{
|
||||||
|
this.setState(prevState => ({seconds: prevState.seconds + 1}))
|
||||||
|
},1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
console.log('Timer unmount');
|
||||||
|
clearInterval(this.interval);
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return <h2>Прошло секунд: {this.state.seconds}</h2>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [show,setShow] = React.useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Таймер с классом</h1>
|
||||||
<h1>Hello, React!</h1>
|
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
||||||
<QueryClientProvider client={createQueryClient}>
|
{show && <Timer />}
|
||||||
<PostAddForm />
|
|
||||||
</QueryClientProvider>
|
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user