Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+58
-74
@@ -1,83 +1,67 @@
|
|||||||
import memo, { useMemo, useCallback, useState } from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
|
|
||||||
function MyComponent() {
|
function tasksReducer(state, action) {
|
||||||
const [count, setCount] = useState(0);
|
switch(action.type) {
|
||||||
const [name, setName] = useState('John');
|
case 'ADD_TASK':
|
||||||
|
return [...state, action.payload]
|
||||||
const greeting = useMemo(() => `Hello, ${name}!`,[name]);
|
case 'TOGGLE_TASK':
|
||||||
|
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||||
|
case 'REMOVE_TASK':
|
||||||
|
return state.filter(task => task.id !== action.payload)
|
||||||
|
default:
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
|
console.log("Rendred")
|
||||||
return (
|
return (
|
||||||
<>
|
<ul>
|
||||||
<h2>{greeting}</h2>
|
{tasks.map(({id, text, complite}) => (
|
||||||
<button onClick={()=>setName('Alex')}>New name</button>
|
<li key={id}
|
||||||
</>
|
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
||||||
|
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
||||||
|
{text}
|
||||||
|
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
||||||
|
Удалить
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
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 [tasks, dispatch] = useReducer(tasksReducer,[])
|
||||||
|
const [input,setInput] = useState('')
|
||||||
|
const [error,setError] = useState('')
|
||||||
|
const inputRef = useRef(null)
|
||||||
|
|
||||||
|
const addTask = () => {
|
||||||
|
if (input.trim() === '') {
|
||||||
|
setError('Пустой текст задачи')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setError('')
|
||||||
|
dispatch({
|
||||||
|
type: 'ADD_TASK',
|
||||||
|
payload: {id: Date.now(), text: input.trim(), complite: false }
|
||||||
|
})
|
||||||
|
setInput('')
|
||||||
|
inputRef.current.focus()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div style={{padding: 20}}>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Список дел</h1>
|
||||||
<h1>Hello, React!</h1>
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
<ParentComponent items={[{name:'Alex'},{name:'Bob'}]} />
|
<button onClick={addTask}>Добавить</button>
|
||||||
</>
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
|
|
||||||
|
<TaskList tasks={tasks}
|
||||||
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
|
<StrictMode>
|
||||||
<App />
|
<App />
|
||||||
|
</StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user