Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6827c800f1 | |||
| 5ee1bf2eee | |||
| c137c644e3 |
+42
-66
@@ -1,82 +1,58 @@
|
|||||||
import memo, { useMemo, useCallback, useState } from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function MyComponent() {
|
class RandomUser extends React.Component {
|
||||||
const [count, setCount] = useState(0);
|
constructor(props) {
|
||||||
const [name, setName] = useState('John');
|
super(props)
|
||||||
|
this.state = { user: {} }
|
||||||
|
}
|
||||||
|
|
||||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
componentDidMount() {
|
||||||
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
console.log('смонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
componentDidUpdate(prevProps,prevState) {
|
||||||
|
console.log(this.state);
|
||||||
|
console.log('Компонент обнвлен')
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
console.log('Компонент таймера размонтирован')
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <div>
|
||||||
|
{this.state.user &&
|
||||||
|
this.state.user.name &&
|
||||||
|
(
|
||||||
<>
|
<>
|
||||||
<h2>{greeting}</h2>
|
<h2>{this.state.user.name.first} {this.state.user.name.last}</h2>
|
||||||
<button onClick={()=>setName('Alex')}>New name</button>
|
<img src={this.state.user.picture.medium} />
|
||||||
|
<button onClick={() => {
|
||||||
|
console.log('Загружаем ... ')
|
||||||
|
fetch('https://randomuser.me/api')
|
||||||
|
.then(responce => responce.json())
|
||||||
|
.then(json => this.setState({ user: json.results[0] }));
|
||||||
|
}}>Загрузить нового</button>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
</div>
|
||||||
function MyComponent2() {
|
|
||||||
const [data,setData] = useState([{theme:'light'}])
|
|
||||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
|
||||||
|
|
||||||
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>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function MyComponent3() {
|
|
||||||
const sum = useMemo(()=> a+b,[a,b]);//Бессмысленное применение и вредное
|
|
||||||
|
|
||||||
//Применение оправдано
|
|
||||||
const complexResult = useMemo(()=>{
|
|
||||||
return heavyMethOperations(largeDataSet);
|
|
||||||
},[largeDataSet])
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
function App() {
|
||||||
|
const [showClock, setShowClock] = React.useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Домашнее задание: жизненый цикл</h1>
|
||||||
<h1>Hello, React!</h1>
|
<button onClick={()=> setShowClock(!showClock)}>
|
||||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
{showClock ? 'Скрыть часы' : 'Показать часы' }
|
||||||
|
</button>
|
||||||
|
{showClock && <RandomUser />}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user