Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aeb40b20 |
+31
-83
@@ -1,96 +1,44 @@
|
|||||||
import React from 'react'
|
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
||||||
import styles from './App.module.scss'
|
import PostAddForm from './components/PostAddForm'
|
||||||
|
|
||||||
class Clock extends React.Component {
|
/*
|
||||||
constructor(props) {
|
const mutation = useMutation({
|
||||||
super(props);
|
mutationFn: newUser => {
|
||||||
this.state = { date: new Date() };
|
fetch('https://jsonplaceholder.typicode.com/users', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {'Content-type':'application/json'},
|
||||||
|
body: JSON.stringify(newUser),
|
||||||
|
}).then(res => res.json())
|
||||||
}
|
}
|
||||||
componentDidMount() {
|
})
|
||||||
this.timerId = setInterval( () => this.tick(), 1000);
|
*/
|
||||||
}
|
|
||||||
componentWillUnmount() {
|
|
||||||
clearInterval(this.timerId)
|
|
||||||
}
|
|
||||||
tick() {
|
|
||||||
this.setState({ date: new Date() })
|
|
||||||
}
|
|
||||||
render() {
|
|
||||||
return <div>{this.state.date.toLocaleTimeString()}</div>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Clock2() {
|
const createQueryClient = () => {
|
||||||
const [date, setDate] = React.useState(new Date());
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
React.useEffect(()=>{
|
queries: {
|
||||||
const timerId = setInterval(()=> setDate(new Date()),1000);
|
staleTime: 5 * 60*1000,
|
||||||
return () => clearInterval(timerId);
|
retry: 1,
|
||||||
},[])
|
gcTime: 10*60*1000,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
return <div>{date.toLocaleTimeString()}</div>
|
},
|
||||||
}
|
mutations: {
|
||||||
|
retry:0,
|
||||||
function MouseTracker() {
|
onError: error => {console.error('Mutation error:',error.message)}
|
||||||
const [x,setX] = React.useState(0);
|
}
|
||||||
const [y,setY] = React.useState(0);
|
|
||||||
|
|
||||||
const mouseMoveHandler = event => {
|
|
||||||
setX(event.clientX);
|
|
||||||
setY(event.clientY);
|
|
||||||
}
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
document.addEventListener('mousemove', mouseMoveHandler);
|
|
||||||
return () => {
|
|
||||||
document.removeEventListener('mousemove',mouseMoveHandler);
|
|
||||||
}
|
}
|
||||||
},[]);
|
})
|
||||||
|
|
||||||
return <div className={styles.container}>
|
|
||||||
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
function DataFetcher() {
|
|
||||||
const [posts,setPosts] = React.useState([]);
|
|
||||||
const [users,setUsers] = React.useState([]);
|
|
||||||
|
|
||||||
const getPosts = async () => {
|
|
||||||
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
|
|
||||||
const data = await response.json();
|
|
||||||
setUsers(data.reduce((acc, value) => {
|
|
||||||
if (!(value.userId in acc))
|
|
||||||
return [...acc,value.userId]
|
|
||||||
return acc
|
|
||||||
}))
|
|
||||||
console.log(users);
|
|
||||||
setPosts(data.slice(1,10));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
console.log("Компонент создан");
|
|
||||||
getPosts();
|
|
||||||
return () => {console.log("Компонент удален");setPosts([])};
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return <div><ol>{posts.map(post => <li id={post.id}>{post.title}</li>)}</ol></div>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tracker,setTracker] = React.useState(false);
|
|
||||||
const [dataFetcher,setDataFetcher] = React.useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<h1>Hello, React!</h1>
|
<h1>Hello, React!</h1>
|
||||||
<Clock />
|
<QueryClientProvider client={createQueryClient}>
|
||||||
<Clock2 />
|
<PostAddForm />
|
||||||
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
</QueryClientProvider>
|
||||||
{ tracker && <MouseTracker /> }
|
|
||||||
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
|
||||||
{ dataFetcher && <DataFetcher /> }
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
.container {
|
|
||||||
width: 300px;
|
|
||||||
height: 200px;
|
|
||||||
border: 2px dashed grey;
|
|
||||||
margin: 10px;
|
|
||||||
text-align: center;
|
|
||||||
p {
|
|
||||||
margin-top: 20%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user