Compare commits

..

3 Commits

3 changed files with 75 additions and 90 deletions
+58 -88
View File
@@ -1,108 +1,78 @@
import React from 'react' import { useState, createContext, useContext } from 'react'
import styles from './App.module.scss'
class MyComponent extends React.Component { const UserContext = createContext('')
constructor(props) {
super(props); function UserProvider({ children }) {
this.state = { data: null }; const [name,setName] = useState('');
this.handleClick = this.handleClick.bind(this);
const toggleName = (newName) => {
setName(newName);
} }
render() { return (
return ( <UserContext.Provider value={{name, toggleName}}>
<div> {children}
{this.state.data ? ( </UserContext.Provider>
<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 { function Form() {
controller = new AbortController() const {name, toggleName} = useContext(UserContext)
const [newName,setNewName] = useState('');
componentDidMount() { const handleSubmit = (event) => {
fetch('https://api.example.com/data',{ singnal: this.controller.signal }) event.preventDefault();
.then(responce => responce.json()) toggleName(event.target[0].value)
.then(json => this.setState({data: json}))
.catch(error => {
if(error.name == 'AbortError') {
console.log('Aborted');
} else {
console.log('Other error',error);
}
});
} }
const handlerChange = (event) => {
componentWillUnmount() { setNewName(event.target.value);
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');
}
} }
return (
<form onSubmit={handleSubmit}>
<input type='text' value={newName} onChange={handlerChange} />
<button type='submit'>Войти</button>
</form>
)
} }
class Timer extends React.Component { function ExitButton() {
constructor(props) { const {name, toggleName} = useContext(UserContext)
super(props);
this.state = { seconds: 0 }
}
componentDidMount() { return (
console.log('Timer mounted'); <button onClick={() => toggleName('')}>Выход</button>
this.interval = setInterval(()=>{ )
this.setState(prevState => ({seconds: prevState.seconds + 1})) }
},1000);
}
componentWillUnmount() { function Toolbar() {
console.log('Timer unmount'); const {name, toggleName} = useContext(UserContext)
clearInterval(this.interval); return(
} <div className={styles.container}>
render() { <h3>Панель пользователя</h3>
return <h2>Прошло секунд: {this.state.seconds}</h2> {name && `Привет ${name}`}
} {name ? <ExitButton /> : <Form /> }
</div>
)
}
function Page() {
return (
<div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
<Toolbar />
</div>
)
} }
function App() { function App() {
const [show,setShow] = React.useState(true);
return ( return (
<> <>
<h1>Таймер с классом</h1> {/* Добавьте тестовый контент для проверки */}
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button> <h1>Hello, React!</h1>
{show && <Timer />} <UserProvider>
<Page />
</UserProvider>
</> </>
) )
} }
+13
View File
@@ -0,0 +1,13 @@
.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,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>
) )