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