Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23db798577 | |||
| c99a045400 |
+39
-47
@@ -1,58 +1,50 @@
|
|||||||
import React from 'react'
|
import { useState, useReducer } from 'react'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
function counterReducer(state, action) {
|
||||||
constructor(props) {
|
switch (action.type) {
|
||||||
super(props)
|
case 'increment':
|
||||||
this.state = { user: {} }
|
return state + 1
|
||||||
}
|
case 'decrement':
|
||||||
|
return state - 1
|
||||||
componentDidMount() {
|
case 'reset':
|
||||||
fetch('https://randomuser.me/api')
|
return 0
|
||||||
.then(responce => responce.json())
|
case 'set_value':
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
return action.value
|
||||||
console.log('смонтирован')
|
default:
|
||||||
}
|
return state
|
||||||
|
|
||||||
componentDidUpdate(prevProps,prevState) {
|
|
||||||
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 Counter() {
|
||||||
|
const [count, dispatch] = useReducer(counterReducer,0);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<span>Счетчик: {count}</span>
|
||||||
|
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
||||||
|
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
||||||
|
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
||||||
|
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [showClock, setShowClock] = React.useState(true);
|
const [tasks, setTasks] = useState([])
|
||||||
|
const [filter, setFilter] = useState('all')
|
||||||
|
const [sortBy,setSortBy] = useState('date')
|
||||||
|
|
||||||
|
const addTask = newTask => {
|
||||||
|
setTasks([...tasks,newTask])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Домашнее задание: жизненый цикл</h1>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<h1>Hello, React!</h1>
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<Counter />
|
||||||
</button>
|
|
||||||
{showClock && <RandomUser />}
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
.container {
|
||||||
|
/* display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
flex-wrap: nowrap;*/
|
||||||
|
height: 500px;
|
||||||
|
overflow: auto;
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user