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