Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2775fc8283 | |||
| c9759fbfc7 |
+75
-37
@@ -1,58 +1,96 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
class RandomUser extends React.Component {
|
class Clock extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props);
|
||||||
this.state = { user: {} }
|
this.state = { date: new Date() };
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
fetch('https://randomuser.me/api')
|
this.timerId = setInterval( () => this.tick(), 1000);
|
||||||
.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() {
|
||||||
console.log('Компонент таймера размонтирован')
|
clearInterval(this.timerId)
|
||||||
|
}
|
||||||
|
tick() {
|
||||||
|
this.setState({ date: new Date() })
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return <div>{this.state.date.toLocaleTimeString()}</div>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
function Clock2() {
|
||||||
return <div>
|
const [date, setDate] = React.useState(new Date());
|
||||||
{this.state.user &&
|
|
||||||
this.state.user.name &&
|
React.useEffect(()=>{
|
||||||
(
|
const timerId = setInterval(()=> setDate(new Date()),1000);
|
||||||
<>
|
return () => clearInterval(timerId);
|
||||||
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
},[])
|
||||||
<img src={this.state.user.picture.medium} />
|
|
||||||
<button onClick={() => {
|
return <div>{date.toLocaleTimeString()}</div>
|
||||||
console.log('Загружаем ... ')
|
|
||||||
fetch('https://randomuser.me/api')
|
|
||||||
.then(responce => responce.json())
|
|
||||||
.then(json => this.setState({ user: json.results[0] }));
|
|
||||||
}}>Загрузить нового</button>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 [showClock, setShowClock] = React.useState(true);
|
const [tracker,setTracker] = React.useState(false);
|
||||||
|
const [dataFetcher,setDataFetcher] = React.useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Домашнее задание: жизненый цикл</h1>
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={()=> setShowClock(!showClock)}>
|
<Clock />
|
||||||
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
<Clock2 />
|
||||||
</button>
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
||||||
{showClock && <RandomUser />}
|
{ tracker && <MouseTracker /> }
|
||||||
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
||||||
|
{ dataFetcher && <DataFetcher /> }
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.container {
|
||||||
|
width: 300px;
|
||||||
|
height: 200px;
|
||||||
|
border: 2px dashed grey;
|
||||||
|
margin: 10px;
|
||||||
|
text-align: center;
|
||||||
|
p {
|
||||||
|
margin-top: 20%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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