Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 10e56d8aaa |
+64
-90
@@ -1,108 +1,82 @@
|
||||
import React from 'react'
|
||||
import memo, { useMemo, useCallback, useState } from 'react'
|
||||
|
||||
class MyComponent extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { data: null };
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
}
|
||||
function MyComponent() {
|
||||
const [count, setCount] = useState(0);
|
||||
const [name, setName] = useState('John');
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
{this.state.data ? (
|
||||
<div>Данные: {this.state.data}</div>
|
||||
) : (
|
||||
<div>Загрузка...</div>
|
||||
)}
|
||||
<button onClick={this.handleClick}>Обновить</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://api.example.com/data')
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({data: json}));
|
||||
|
||||
this.timerId = setInterval(()=> console.log('Tick'), 1000);
|
||||
}
|
||||
|
||||
componentWillUnmout() {
|
||||
clearInterval(this.timerId);
|
||||
}
|
||||
|
||||
handleClick() {
|
||||
this.setState({data: 'Данные'})
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<h2>{greeting}</h2>
|
||||
<button onClick={()=>setName('Alex')}>New name</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
class MyComponent2 extends React.Component {
|
||||
controller = new AbortController()
|
||||
function MyComponent2() {
|
||||
const [data,setData] = useState([{theme:'light'}])
|
||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://api.example.com/data',{ singnal: this.controller.signal })
|
||||
.then(responce => responce.json())
|
||||
.then(json => this.setState({data: json}))
|
||||
.catch(error => {
|
||||
if(error.name == 'AbortError') {
|
||||
console.log('Aborted');
|
||||
} else {
|
||||
console.log('Other error',error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.controller.abort()
|
||||
}
|
||||
}
|
||||
class MyComponent3 extends React.Component {
|
||||
componentDidMount() {
|
||||
this.loadData();
|
||||
}
|
||||
|
||||
async loadData() {
|
||||
try {
|
||||
const response = await fetch('https://api.example.com/data');
|
||||
const data = await response.json();
|
||||
this.setSate({ data });
|
||||
} catch {
|
||||
console.log('error');
|
||||
}
|
||||
}
|
||||
const processData = useMemo(()=>{
|
||||
return data.map(item => ({ ...item,theme: userPreferences.theme }))
|
||||
},[data, userPreferences]);
|
||||
console.log(processData);
|
||||
return (
|
||||
<>
|
||||
<h2 className={data.theme}>2</h2>
|
||||
<button onClick={()=>setData(data)}>set new theme</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
class Timer extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { seconds: 0 }
|
||||
}
|
||||
function MyComponent3() {
|
||||
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
||||
|
||||
componentDidMount() {
|
||||
console.log('Timer mounted');
|
||||
this.interval = setInterval(()=>{
|
||||
this.setState(prevState => ({seconds: prevState.seconds + 1}))
|
||||
},1000);
|
||||
}
|
||||
//Применение оправдано
|
||||
const complexResult = useMemo(()=>{
|
||||
return heavyMethOperations(largeDataSet);
|
||||
},[largeDataSet])
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
console.log('Timer unmount');
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
render() {
|
||||
return <h2>Прошло секунд: {this.state.seconds}</h2>
|
||||
}
|
||||
function ParentComponent({ items }) {
|
||||
const [filter, setFilter] = useState('');
|
||||
|
||||
const listProps = useMemo(()=>({
|
||||
items: items.filter(item => item.name.includes(filter)),
|
||||
onItemClick: id => console.log('Clicked',id),
|
||||
theme: 'dark'
|
||||
}),[items,filter]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Memoized {...listProps} />
|
||||
<button onClick={()=>setFilter('Bob')}>Click</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function Memoized(props) {
|
||||
console.log(props);
|
||||
return (
|
||||
<>
|
||||
<h3 className={props.theme}>Memoized</h3>
|
||||
{props.items.map((item) => (
|
||||
<>
|
||||
<li>
|
||||
{item.name}
|
||||
</li>
|
||||
</>))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function App() {
|
||||
const [show,setShow] = React.useState(true);
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Таймер с классом</h1>
|
||||
<button onClick={()=>setShow(prev => !prev)}>{show ? 'Скрыть' : 'Показать'} таймер</button>
|
||||
{show && <Timer />}
|
||||
{/* Добавьте тестовый контент для проверки */}
|
||||
<h1>Hello, React!</h1>
|
||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user