Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+56
-86
@@ -1,97 +1,67 @@
|
||||
import React from 'react'
|
||||
import styles from './App.module.scss'
|
||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||
|
||||
class Clock extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { date: new Date() };
|
||||
}
|
||||
componentDidMount() {
|
||||
this.timerId = setInterval( () => this.tick(), 1000);
|
||||
}
|
||||
componentWillUnmount() {
|
||||
clearInterval(this.timerId)
|
||||
}
|
||||
tick() {
|
||||
this.setState({ date: new Date() })
|
||||
}
|
||||
render() {
|
||||
return <div>{this.state.date.toLocaleTimeString()}</div>
|
||||
function tasksReducer(state, action) {
|
||||
switch(action.type) {
|
||||
case 'ADD_TASK':
|
||||
return [...state, action.payload]
|
||||
case 'TOGGLE_TASK':
|
||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||
case 'REMOVE_TASK':
|
||||
return state.filter(task => task.id !== action.payload)
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||
console.log("Rendred")
|
||||
return (
|
||||
<ul>
|
||||
{tasks.map(({id, text, complite}) => (
|
||||
<li key={id}
|
||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||
{text}
|
||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||
Удалить
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
})
|
||||
|
||||
function App() {
|
||||
const [tracker,setTracker] = React.useState(false);
|
||||
const [dataFetcher,setDataFetcher] = React.useState(false);
|
||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
||||
const [input,setInput] = useState('')
|
||||
const [error,setError] = useState('')
|
||||
const inputRef = useRef(null)
|
||||
|
||||
const addTask = () => {
|
||||
if (input.trim() === '') {
|
||||
setError('Пустой текст задачи')
|
||||
return
|
||||
}
|
||||
setError('')
|
||||
dispatch({
|
||||
type: 'ADD_TASK',
|
||||
payload: {id: Date.now(), text: input.trim(), complite: false }
|
||||
})
|
||||
setInput('')
|
||||
inputRef.current.focus()
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Hello, React!</h1>
|
||||
<Clock />
|
||||
<Clock2 />
|
||||
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
||||
{ tracker && <MouseTracker /> }
|
||||
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
||||
{ dataFetcher && <DataFetcher /> }
|
||||
</>
|
||||
<div style={{padding: 20}}>
|
||||
<h1>Список дел</h1>
|
||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||
<button onClick={addTask}>Добавить</button>
|
||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||
|
||||
<TaskList tasks={tasks}
|
||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
.container {
|
||||
width: 300px;
|
||||
height: 200px;
|
||||
border: 2px dashed grey;
|
||||
margin: 10px;
|
||||
text-align: center;
|
||||
p {
|
||||
margin-top: 20%;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user