Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c168c96820 | |||
| b70fa47bbc | |||
| bd35029fc1 | |||
| 92dc5df96b | |||
| 3d34262203 | |||
| 1bc2e883a1 | |||
| 8a8a17f44d | |||
| 8f46470a26 |
Generated
+1256
-9
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"info": "^1.0.0",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
"react-dom": "^19.1.0"
|
"react-dom": "^19.1.0"
|
||||||
},
|
},
|
||||||
@@ -22,6 +23,8 @@
|
|||||||
"eslint-plugin-react-hooks": "^5.2.0",
|
"eslint-plugin-react-hooks": "^5.2.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.19",
|
"eslint-plugin-react-refresh": "^0.4.19",
|
||||||
"globals": "^16.0.0",
|
"globals": "^16.0.0",
|
||||||
|
"sass": "^1.99.0",
|
||||||
|
"sass-embedded": "^1.99.0",
|
||||||
"vite": "^6.3.5"
|
"vite": "^6.3.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-70
@@ -1,82 +1,29 @@
|
|||||||
import memo, { useMemo, useCallback, useState } from 'react'
|
import styles from './styles/App.module.scss'
|
||||||
|
import React, { useState, useCallback } from 'react'
|
||||||
function MyComponent() {
|
|
||||||
const [count, setCount] = useState(0);
|
|
||||||
const [name, setName] = useState('John');
|
|
||||||
|
|
||||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
|
||||||
|
|
||||||
|
function CounterButton({label, onClick}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<button onClick={onClick} className={styles.counter_button}>{label}</button>
|
||||||
<h2>{greeting}</h2>
|
|
||||||
<button onClick={()=>setName('Alex')}>New name</button>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
function CounterDisplay({value}) {
|
||||||
function MyComponent2() {
|
return (<div className={styles.counter_display}>
|
||||||
const [data,setData] = useState([{theme:'light'}])
|
Значение счетчика: <b>{value}</b>
|
||||||
const userPreferences = useMemo(()=>({theme: 'dark', language: 'en'}),[]);
|
</div>)
|
||||||
|
|
||||||
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 [counter,setCounter] = useState(0);
|
||||||
|
|
||||||
|
const increment = () => setCounter(prev => prev + 1);
|
||||||
|
const decrement = () => setCounter(prev => prev - 1);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h2>Практика по созданию счетчика с колбэками</h2>
|
||||||
<h1>Hello, React!</h1>
|
<CounterButton label="Увеличить" onClick={increment} />
|
||||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
<CounterButton label="уменьшить" onClick={decrement} />
|
||||||
|
<CounterDisplay value={counter} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -1,8 +1,10 @@
|
|||||||
import { StrictMode } from 'react'
|
import { StrictMode } from 'react'
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from 'react-dom/client'
|
||||||
// удаляем import './index.css'
|
|
||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
<StrictMode>
|
||||||
<App />
|
<App />
|
||||||
|
</StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
.container {
|
||||||
|
display:flex;
|
||||||
|
flex-direction: row;
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
p {
|
||||||
|
padding: 5px 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.counter {
|
||||||
|
&_display{
|
||||||
|
border: 1px solid black;
|
||||||
|
border-width: 3px;
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 10px;
|
||||||
|
width: 300px;
|
||||||
|
}
|
||||||
|
&_button{
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user