Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2775fc8283 | |||
| c9759fbfc7 |
+83
-31
@@ -1,44 +1,96 @@
|
|||||||
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
import React from 'react'
|
||||||
import PostAddForm from './components/PostAddForm'
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
/*
|
class Clock extends React.Component {
|
||||||
const mutation = useMutation({
|
constructor(props) {
|
||||||
mutationFn: newUser => {
|
super(props);
|
||||||
fetch('https://jsonplaceholder.typicode.com/users', {
|
this.state = { date: new Date() };
|
||||||
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>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const createQueryClient = () => {
|
function Clock2() {
|
||||||
new QueryClient({
|
const [date, setDate] = React.useState(new Date());
|
||||||
defaultOptions: {
|
|
||||||
queries: {
|
React.useEffect(()=>{
|
||||||
staleTime: 5 * 60*1000,
|
const timerId = setInterval(()=> setDate(new Date()),1000);
|
||||||
retry: 1,
|
return () => clearInterval(timerId);
|
||||||
gcTime: 10*60*1000,
|
},[])
|
||||||
refetchOnWindowFocus: false,
|
|
||||||
},
|
return <div>{date.toLocaleTimeString()}</div>
|
||||||
mutations: {
|
}
|
||||||
retry:0,
|
|
||||||
onError: error => {console.error('Mutation error:',error.message)}
|
function MouseTracker() {
|
||||||
}
|
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>
|
||||||
<QueryClientProvider client={createQueryClient}>
|
<Clock />
|
||||||
<PostAddForm />
|
<Clock2 />
|
||||||
</QueryClientProvider>
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
||||||
|
{ tracker && <MouseTracker /> }
|
||||||
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
||||||
|
{ dataFetcher && <DataFetcher /> }
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.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