Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12515f925d | |||
| b3662b759f |
+92
-62
@@ -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}}>
|
|
||||||
{children}
|
|
||||||
</UserContext.Provider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Form() {
|
|
||||||
const {name, toggleName} = useContext(UserContext)
|
|
||||||
const [newName,setNewName] = useState('');
|
|
||||||
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 (
|
|
||||||
<button onClick={() => toggleName('')}>Выход</button>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Toolbar() {
|
|
||||||
const {name, toggleName} = useContext(UserContext)
|
|
||||||
return(
|
|
||||||
<div className={styles.container}>
|
|
||||||
<h3>Панель пользователя</h3>
|
|
||||||
{name && `Привет ${name}`}
|
|
||||||
{name ? <ExitButton /> : <Form /> }
|
|
||||||
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Page() {
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
{this.state.data ? (
|
||||||
<Toolbar />
|
<div>Данные: {this.state.data}</div>
|
||||||
|
) : (
|
||||||
|
<div>Загрузка...</div>
|
||||||
|
)}
|
||||||
|
<button onClick={this.handleClick}>Обновить</button>
|
||||||
</div>
|
</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 [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>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user