Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6827c800f1 | |||
| 5ee1bf2eee | |||
| c137c644e3 |
+45
-37
@@ -1,50 +1,58 @@
|
|||||||
import { useState, useReducer } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function counterReducer(state, action) {
|
class RandomUser extends React.Component {
|
||||||
switch (action.type) {
|
constructor(props) {
|
||||||
case 'increment':
|
super(props)
|
||||||
return state + 1
|
this.state = { user: {} }
|
||||||
case 'decrement':
|
|
||||||
return state - 1
|
|
||||||
case 'reset':
|
|
||||||
return 0
|
|
||||||
case 'set_value':
|
|
||||||
return action.value
|
|
||||||
default:
|
|
||||||
return state
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function Counter() {
|
componentDidMount() {
|
||||||
const [count, dispatch] = useReducer(counterReducer,0);
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
console.log('смонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
componentDidUpdate(prevProps,prevState) {
|
||||||
<div>
|
console.log(this.state);
|
||||||
<span>Счетчик: {count}</span>
|
console.log('Компонент обнвлен')
|
||||||
<button onClick={()=> dispatch({type: 'increment'})}>+</button>
|
}
|
||||||
<button onClick={()=> dispatch({type: 'decrement'})}>-</button>
|
|
||||||
<button onClick={()=> dispatch({type: 'reset'})}>reset</button>
|
componentWillUnmount() {
|
||||||
<button onClick={()=> dispatch({type: 'set_value', value: 5})}>set 5</button>
|
console.log('Компонент таймера размонтирован')
|
||||||
</div>
|
}
|
||||||
)
|
|
||||||
|
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 App() {
|
function App() {
|
||||||
const [tasks, setTasks] = useState([])
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
const [filter, setFilter] = useState('all')
|
|
||||||
const [sortBy,setSortBy] = useState('date')
|
|
||||||
|
|
||||||
const addTask = newTask => {
|
|
||||||
setTasks([...tasks,newTask])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<h1>Hello, React!</h1>
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
<Counter />
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
|
</button>
|
||||||
|
{showClock && <RandomUser />}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
.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,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>
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user