Compare commits

..

2 Commits

Author SHA1 Message Date
laktionov-as 12515f925d Сделали таймер 2026-05-28 20:45:57 +03:00
laktionov-as b3662b759f class components 2026-05-28 20:29:26 +03:00
3 changed files with 91 additions and 76 deletions
+91 -61
View File
@@ -1,78 +1,108 @@
import { useState, createContext, useContext } from 'react' import React from 'react'
import styles from './App.module.scss'
const UserContext = createContext('') class MyComponent extends React.Component {
constructor(props) {
function UserProvider({ children }) { super(props);
const [name,setName] = useState(''); this.state = { data: null };
this.handleClick = this.handleClick.bind(this);
const toggleName = (newName) => {
setName(newName);
} }
return ( render() {
<UserContext.Provider value={{name, toggleName}}> return (
{children} <div>
</UserContext.Provider> {this.state.data ? (
) <div>Данные: {this.state.data}</div>
} ) : (
<div>Загрузка...</div>
function Form() { )}
const {name, toggleName} = useContext(UserContext) <button onClick={this.handleClick}>Обновить</button>
const [newName,setNewName] = useState(''); </div>
const handleSubmit = (event) => { )
event.preventDefault();
toggleName(event.target[0].value)
} }
const handlerChange = (event) => {
setNewName(event.target.value);
}
return (
<form onSubmit={handleSubmit}>
<input type='text' value={newName} onChange={handlerChange} />
<button type='submit'>Войти</button>
</form>
)
}
function ExitButton() {
const {name, toggleName} = useContext(UserContext)
return ( componentDidMount() {
<button onClick={() => toggleName('')}>Выход</button> 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: 'Данные'})
}
} }
function Toolbar() { class MyComponent2 extends React.Component {
const {name, toggleName} = useContext(UserContext) controller = new AbortController()
return(
<div className={styles.container}> componentDidMount() {
<h3>Панель пользователя</h3> fetch('https://api.example.com/data',{ singnal: this.controller.signal })
{name && `Привет ${name}`} .then(responce => responce.json())
{name ? <ExitButton /> : <Form /> } .then(json => this.setState({data: json}))
.catch(error => {
</div> 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');
}
}
} }
function Page() { class Timer extends React.Component {
return ( constructor(props) {
<div> super(props);
<h1 style={{textAlign: 'center'}}>Главная страница</h1> this.state = { seconds: 0 }
<Toolbar /> }
</div>
) 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 [show,setShow] = React.useState(true);
return ( return (
<> <>
{/* Добавьте тестовый контент для проверки */} <h1>Таймер с классом</h1>
<h1>Hello, React!</h1> <button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
<UserProvider> {show && <Timer />}
<Page />
</UserProvider>
</> </>
) )
} }
-13
View File
@@ -1,13 +0,0 @@
.container {
height: 500px;
border: 1px solid black;
border-radius: 10px;
background-color: lightblue;
}
.item {
font-size: 16pt;
border: 1px dotted gray;
/*margin: 5px 10px;
padding: 10px;*/
border-radius: 5px;
}
-2
View File
@@ -4,7 +4,5 @@ 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>
) )