|
|
|
@@ -1,59 +1,96 @@
|
|
|
|
|
import { memo, useState, useMemo } from 'react'
|
|
|
|
|
import React from 'react'
|
|
|
|
|
import styles from './App.module.scss'
|
|
|
|
|
|
|
|
|
|
function memoize(fn) {
|
|
|
|
|
const cache = new Map();
|
|
|
|
|
return function(...args) {
|
|
|
|
|
const key = JSON.stringify(args);
|
|
|
|
|
if (cache.has(key)) {
|
|
|
|
|
return cache.get(key);
|
|
|
|
|
class Clock extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = { date: new Date() };
|
|
|
|
|
}
|
|
|
|
|
const result = fn.apply(this, args);
|
|
|
|
|
cache.set(key,result);
|
|
|
|
|
return result;
|
|
|
|
|
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 fibonachi(n, memo={}) {
|
|
|
|
|
if (n in memo) {console.log(n);return memo[n];}
|
|
|
|
|
if (n <= 1) return 1;
|
|
|
|
|
memo[n] = fibonachi(n-1, memo) + fibonachi(n - 2, memo);
|
|
|
|
|
return memo[n];
|
|
|
|
|
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>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MyComponent = ({ value }) => {
|
|
|
|
|
console.log('Rendring');
|
|
|
|
|
return <div>{value}</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>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MemoizedComponent = memo(MyComponent);
|
|
|
|
|
function DataFetcher() {
|
|
|
|
|
const [posts,setPosts] = React.useState([]);
|
|
|
|
|
const [users,setUsers] = React.useState([]);
|
|
|
|
|
|
|
|
|
|
const Child = memo(({ children }) => {
|
|
|
|
|
console.log('Child component rendred');
|
|
|
|
|
return <div>{children}</div>;
|
|
|
|
|
});
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
const Parent = () => {
|
|
|
|
|
const [count,setCount] = useState(0);
|
|
|
|
|
const [text, setText] = useState('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stableChild = useMemo(() => <span>Memo text</span>,[]);
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
console.log("Компонент создан");
|
|
|
|
|
getPosts();
|
|
|
|
|
return () => {console.log("Компонент удален");setPosts([])};
|
|
|
|
|
},[])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Child>
|
|
|
|
|
{stableChild}
|
|
|
|
|
</Child>
|
|
|
|
|
<input type="text" onChange={(e) => setText(e.target.value)} value={text} />
|
|
|
|
|
<button onClick={() => setCount(count + 1)}>Increment {count}</button>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
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 (
|
|
|
|
|
<>
|
|
|
|
|
<h1>Hello, React!</h1>
|
|
|
|
|
<Parent />
|
|
|
|
|
<Clock />
|
|
|
|
|
<Clock2 />
|
|
|
|
|
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
|
|
|
|
|
{ tracker && <MouseTracker /> }
|
|
|
|
|
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
|
|
|
|
|
{ dataFetcher && <DataFetcher /> }
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|