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