Compare commits

..

3 Commits

3 changed files with 45 additions and 80 deletions
+42 -62
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); componentDidUpdate(prevProps,prevState) {
console.log(this.state);
console.log('Компонент обнвлен')
} }
return (
<form onSubmit={handleSubmit}>
<input type='text' value={newName} onChange={handlerChange} />
<button type='submit'>Войти</button>
</form>
)
}
function ExitButton() { componentWillUnmount() {
const {name, toggleName} = useContext(UserContext) console.log('Компонент таймера размонтирован')
}
return ( render() {
<button onClick={() => toggleName('')}>Выход</button> return <div>
) {this.state.user &&
} this.state.user.name &&
(
function Toolbar() { <>
const {name, toggleName} = useContext(UserContext) <h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
return( <img src={this.state.user.picture.medium} />
<div className={styles.container}> <button onClick={() => {
<h3>Панель пользователя</h3> console.log('Загружаем ... ')
{name && `Привет ${name}`} fetch('https://randomuser.me/api')
{name ? <ExitButton /> : <Form /> } .then(responce => responce.json())
.then(json => this.setState({ user: json.results[0] }));
</div> }}>Загрузить нового</button>
) </>
} )
}
function Page() { </div>
return ( }
<div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
<Toolbar />
</div>
)
} }
function App() { function App() {
const [showClock, setShowClock] = React.useState(true);
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>
) )