Compare commits

..

3 Commits

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