Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2775fc8283 | |||
| c9759fbfc7 |
Generated
-11
@@ -8,7 +8,6 @@
|
|||||||
"name": "store-client",
|
"name": "store-client",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -2055,16 +2054,6 @@
|
|||||||
"node": ">= 4"
|
"node": ">= 4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/immer": {
|
|
||||||
"version": "11.1.8",
|
|
||||||
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
|
|
||||||
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
|
|
||||||
"license": "MIT",
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/immer"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/import-fresh": {
|
"node_modules/import-fresh": {
|
||||||
"version": "3.3.1",
|
"version": "3.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||||
|
|||||||
+1
-2
@@ -4,13 +4,12 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"immer": "^11.1.8",
|
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+88
-41
@@ -1,49 +1,96 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import React from 'react'
|
||||||
import { produce } from 'immer'
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
|
class Clock extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { date: new Date() };
|
||||||
|
}
|
||||||
|
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 [date, setDate] = React.useState(new Date());
|
||||||
|
|
||||||
|
React.useEffect(()=>{
|
||||||
|
const timerId = setInterval(()=> setDate(new Date()),1000);
|
||||||
|
return () => clearInterval(timerId);
|
||||||
|
},[])
|
||||||
|
|
||||||
|
return <div>{date.toLocaleTimeString()}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
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 [tasks,setTasks] = useState([])
|
const [tracker,setTracker] = React.useState(false);
|
||||||
const [input,setInput] = useState('')
|
const [dataFetcher,setDataFetcher] = React.useState(false);
|
||||||
|
|
||||||
const addTask = () => {
|
|
||||||
if(!input.trim()) return
|
|
||||||
|
|
||||||
setTasks(currentTasks => produce(currentTasks, draft => {
|
|
||||||
draft.push({
|
|
||||||
id: Date.now(),
|
|
||||||
text: input.trim(),
|
|
||||||
done: false
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
setInput('')
|
|
||||||
}
|
|
||||||
const toggleTask = id => {
|
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
|
||||||
const task = draft.find(t => t.id === id)
|
|
||||||
if(task) {
|
|
||||||
task.done = !task.done
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
const removeTask = id => {
|
|
||||||
setTasks(currentTask => produce(currentTask, draft => {
|
|
||||||
const index = draft.findIndex(t => t.id === id)
|
|
||||||
if (index !== -1)
|
|
||||||
draft.splice(index,1)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Практика по созданию списка задач</h1>
|
<h1>Hello, React!</h1>
|
||||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
<Clock />
|
||||||
<button onClick={addTask}>Добавить</button>
|
<Clock2 />
|
||||||
<ul>{tasks.map(({id,text,done}) => (
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
||||||
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
{ tracker && <MouseTracker /> }
|
||||||
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
||||||
{text}
|
{ dataFetcher && <DataFetcher /> }
|
||||||
<button onClick={() => removeTask(id)}>Удалить</button>
|
|
||||||
</li>
|
|
||||||
))}</ul>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.container {
|
||||||
|
width: 300px;
|
||||||
|
height: 200px;
|
||||||
|
border: 2px dashed grey;
|
||||||
|
margin: 10px;
|
||||||
|
text-align: center;
|
||||||
|
p {
|
||||||
|
margin-top: 20%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
export function MyComponent() {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<h1>Hello</h1>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export default MyComponent
|
|
||||||
@@ -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