Compare commits

..

3 Commits

3 changed files with 82 additions and 47 deletions
+62 -42
View File
@@ -1,58 +1,78 @@
import React from 'react' import { useState, createContext, useContext } from 'react'
import styles from './App.module.scss'
class RandomUser extends React.Component { const UserContext = createContext('')
constructor(props) {
super(props) function UserProvider({ children }) {
this.state = { user: {} } const [name,setName] = useState('');
const toggleName = (newName) => {
setName(newName);
} }
componentDidMount() { return (
fetch('https://randomuser.me/api') <UserContext.Provider value={{name, toggleName}}>
.then(responce => responce.json()) {children}
.then(json => this.setState({ user: json.results[0] })); </UserContext.Provider>
console.log('смонтирован') )
} }
componentDidUpdate(prevProps,prevState) { function Form() {
console.log(this.state); const {name, toggleName} = useContext(UserContext)
console.log('Компонент обнвлен') 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>
)
}
componentWillUnmount() { function ExitButton() {
console.log('Компонент таймера размонтирован') const {name, toggleName} = useContext(UserContext)
}
render() { return (
return <div> <button onClick={() => toggleName('')}>Выход</button>
{this.state.user && )
this.state.user.name && }
(
<> function Toolbar() {
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2> const {name, toggleName} = useContext(UserContext)
<img src={this.state.user.picture.medium} /> return(
<button onClick={() => { <div className={styles.container}>
console.log('Загружаем ... ') <h3>Панель пользователя</h3>
fetch('https://randomuser.me/api') {name && `Привет ${name}`}
.then(responce => responce.json()) {name ? <ExitButton /> : <Form /> }
.then(json => this.setState({ user: json.results[0] }));
}}>Загрузить нового</button> </div>
</> )
) }
}
</div> function Page() {
} return (
<div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
<Toolbar />
</div>
)
} }
function App() { function App() {
const [showClock, setShowClock] = React.useState(true);
return ( return (
<> <>
<h1>Домашнее задание: жизненый цикл</h1> {/* Добавьте тестовый контент для проверки */}
<button onClick={()=> setShowClock(!showClock)}> <h1>Hello, React!</h1>
{showClock ? 'Скрыть часы' : 'Показать часы' } <UserProvider>
</button> <Page />
{showClock && <RandomUser />} </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>
) )