Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6827c800f1 | |||
| 5ee1bf2eee | |||
| c137c644e3 |
+37
-75
@@ -1,96 +1,58 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import styles from './App.module.scss'
|
|
||||||
|
|
||||||
class Clock extends React.Component {
|
class RandomUser extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props)
|
||||||
this.state = { date: new Date() };
|
this.state = { user: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.timerId = setInterval( () => this.tick(), 1000);
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
console.log('смонтирован')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(prevProps,prevState) {
|
||||||
|
console.log(this.state);
|
||||||
|
console.log('Компонент обнвлен')
|
||||||
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
clearInterval(this.timerId)
|
console.log('Компонент таймера размонтирован')
|
||||||
}
|
|
||||||
tick() {
|
|
||||||
this.setState({ date: new Date() })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div>{this.state.date.toLocaleTimeString()}</div>
|
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>
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function Clock2() {
|
|
||||||
const [date, setDate] = React.useState(new Date());
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
const timerId = setInterval(()=> setDate(new Date()),1000);
|
|
||||||
return () => clearInterval(timerId);
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return <div>{date.toLocaleTimeString()}</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
function MouseTracker() {
|
|
||||||
const [x,setX] = React.useState(0);
|
|
||||||
const [y,setY] = React.useState(0);
|
|
||||||
|
|
||||||
const mouseMoveHandler = event => {
|
|
||||||
setX(event.clientX);
|
|
||||||
setY(event.clientY);
|
|
||||||
}
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
document.addEventListener('mousemove', mouseMoveHandler);
|
|
||||||
return () => {
|
|
||||||
document.removeEventListener('mousemove',mouseMoveHandler);
|
|
||||||
}
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
return <div className={styles.container}>
|
|
||||||
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
|
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
function DataFetcher() {
|
|
||||||
const [posts,setPosts] = React.useState([]);
|
|
||||||
const [users,setUsers] = React.useState([]);
|
|
||||||
|
|
||||||
const getPosts = async () => {
|
|
||||||
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
|
|
||||||
const data = await response.json();
|
|
||||||
setUsers(data.reduce((acc, value) => {
|
|
||||||
if (!(value.userId in acc))
|
|
||||||
return [...acc,value.userId]
|
|
||||||
return acc
|
|
||||||
}))
|
|
||||||
console.log(users);
|
|
||||||
setPosts(data.slice(1,10));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
console.log("Компонент создан");
|
|
||||||
getPosts();
|
|
||||||
return () => {console.log("Компонент удален");setPosts([])};
|
|
||||||
},[])
|
|
||||||
|
|
||||||
return <div><ol>{posts.map(post => <li id={post.id}>{post.title}</li>)}</ol></div>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [tracker,setTracker] = React.useState(false);
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
const [dataFetcher,setDataFetcher] = React.useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Hello, React!</h1>
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<Clock />
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
<Clock2 />
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
</button>
|
||||||
{ tracker && <MouseTracker /> }
|
{showClock && <RandomUser />}
|
||||||
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
|
||||||
{ dataFetcher && <DataFetcher /> }
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
.container {
|
|
||||||
width: 300px;
|
|
||||||
height: 200px;
|
|
||||||
border: 2px dashed grey;
|
|
||||||
margin: 10px;
|
|
||||||
text-align: center;
|
|
||||||
p {
|
|
||||||
margin-top: 20%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -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