Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12515f925d | |||
| b3662b759f |
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",
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
+100
-41
@@ -1,49 +1,108 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import React from 'react'
|
||||||
import { produce } from 'immer'
|
|
||||||
|
class MyComponent extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { data: null };
|
||||||
|
this.handleClick = this.handleClick.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
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: 'Данные'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyComponent2 extends React.Component {
|
||||||
|
controller = new AbortController()
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({data: json}))
|
||||||
|
.catch(error => {
|
||||||
|
if(error.name == 'AbortError') {
|
||||||
|
console.log('Aborted');
|
||||||
|
} else {
|
||||||
|
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 [tasks,setTasks] = useState([])
|
const [show,setShow] = React.useState(true);
|
||||||
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>
|
||||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
||||||
<button onClick={addTask}>Добавить</button>
|
{show && <Timer />}
|
||||||
<ul>{tasks.map(({id,text,done}) => (
|
|
||||||
<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>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
export function MyComponent() {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<h1>Hello</h1>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export default MyComponent
|
|
||||||
Reference in New Issue
Block a user