Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de33066cbe |
Generated
+11
@@ -8,6 +8,7 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
@@ -2054,6 +2055,16 @@
|
|||||||
"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",
|
||||||
|
|||||||
+2
-1
@@ -4,12 +4,13 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
+41
-50
@@ -1,58 +1,49 @@
|
|||||||
import React from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
|
import { produce } from 'immer'
|
||||||
class RandomUser extends React.Component {
|
|
||||||
constructor(props) {
|
|
||||||
super(props)
|
|
||||||
this.state = { user: {} }
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
fetch('https://randomuser.me/api')
|
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
|
||||||
console.log('смонтирован')
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate(prevProps,prevState) {
|
|
||||||
console.log(this.state);
|
|
||||||
console.log('Компонент обнвлен')
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
const [tasks,setTasks] = useState([])
|
||||||
|
const [input,setInput] = useState('')
|
||||||
|
|
||||||
|
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>Практика по созданию списка задач</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<input value={input} onChange={e=>setInput(e.target.value)} />
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<button onClick={addTask}>Добавить</button>
|
||||||
</button>
|
<ul>{tasks.map(({id,text,done}) => (
|
||||||
{showClock && <RandomUser />}
|
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
||||||
|
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
||||||
|
{text}
|
||||||
|
<button onClick={() => removeTask(id)}>Удалить</button>
|
||||||
|
</li>
|
||||||
|
))}</ul>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export function MyComponent() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Hello</h1>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
export default MyComponent
|
||||||
Reference in New Issue
Block a user